Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:44:18+00:00 2026-06-10T15:44:18+00:00

I have an array: x = [a, b, c, d, e, f, g, h]

  • 0

I have an array: x = [a, b, c, d, e, f, g, h] which can have objects from 1 to 9

Firstly, I have to count IF any of these objects is present 3 times. I don’t want to write

if (x.count(1) == 3) or (x.count(2) == 3) ...etc... 

is there a way to shorten this, like below?

x.count { |obj| obj } == 3 

Secondly, if I know that an object has been found with 3 instances, how can I find out which one was it? (1 or 2 or 3…..)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:44:20+00:00Added an answer on June 10, 2026 at 3:44 pm
    x = [:a, :b, :b, :b, :c, :c, :c]
    counted = Hash[
      x.group_by do |e|
        x.count(e)
      end.map do |count, items|
        [count, items.uniq]
      end
    ]
    p counted[3]    #=> [:b, :c]
    

    How does this work? Let’s follow the steps. First, let’s group the items by count:

    grouped_by_count = x.group_by do |e|
      x.count(e)
    end
    

    This produces a hash with the keys being the counts, and the values being the list of non-unique items having that count:

    p grouped_by_count
    #=> {1=>[:a], 3=>[:b, :b, :b, :c, :c, :c]}
    

    We’d really rather have unique items, though, so let’s do that transform:

    grouped_by_count_unique = grouped_by_count.map do |count, items|
      [count, items.uniq]
    end
    p grouped_by_count_unique
    #=> [[1, [:a]], [3, [:b, :c]]]
    

    That gives us an array of arrays, and not a hash. Fortunately, it’s easy to turn an array of arrays into a hash:

    counted = Hash[grouped_by_count_unique]
    p counted
    # => {1=>[:a], 3=>[:b, :c]}
    

    Now just put the pieces together eliminating the temporaries and you get the answer at the top.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have an Array of Objects which I want to filter depending on some
I have an array of objects which all derive from the class BaseStudent. BaseStudent**studentlist
hi i have an array which holds 200 objects or so. Each of these
I have an array which contains around 50-200 hyperlinks. How can I pass this
I have a generated nested Array which I store some data. How can i
I have an array which I want to use in a query: Array (
I have an array which contains 12 numbers and I want to save this
I have two arrays which contain objects of assets, now I want to subtract
In my code I have an array of objects which are UIImageViews with UIImages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.