0

Lets say array look like below

city = ['london', 'new york', 'london', 'london', 'washington']

desired_location = ['london']

city & desired_location gives ['london']

but I want ['london', 'london', 'london']

3
  • Ok, you have some ambition/goal. Now you need to tell SO what you need help with, where you are stuck. Commented Mar 2, 2015 at 17:14
  • Basically, I need to take common element from two array and take top repeated element from common element. Commented Mar 2, 2015 at 17:17
  • @HariKrishnan, You have to be very specific about what you need. From your example, all that we can say is get all objects from city, which is also present in desired_locations. That is exactly what my solution does. You have not mentioned anything about top repeated elements, in your question. We could not have guessed that. Commented Mar 3, 2015 at 4:51

3 Answers 3

2

You can use Enumerable#select

city.select {|c| desired_location.include?(c)}
# => ["london", "london", "london"]
Sign up to request clarification or add additional context in comments.

4 Comments

i think this will work, but both array will contain 1000 + element. is it feasible ?
compare with desired location ?, what Santhosh put works. city.select {|c| desired_location.include?(c)} will return ['london', 'london', 'london']. If desired_location is desired_location = ['london', 'new york'], you would get ["london", "new york", "london", "london"]
What didn't work? What happened when you tried it? Did you have a typo or syntax error? This should work.
when i tried this. it didn't get repeated element from first array. lemme try again.
1
cities = ['london', 'new york', 'london', 'london', 'washington']

If desired_location contains a single element:

desired_location = ['london']

I recommend @santosh's solution, but this also works:

desired_location.flat_map { |c| [c]*cities.count(c) }
  #=> ["london", "london", "london"]

Suppose desired_location contains multiple elements (which I assume is a possibility, for otherwise there would be no need for it to be an array):

desired_location = ['london', 'new york']

@Santosh' method returns:

["london", "new York", "london", "london"]

which is quite possibly what you want. If you'd prefer that they be grouped:

desired_location.flat_map { |c| [c]*cities.count(c) }
  #=> ["london", "london", "london", "new york"]

or:

desired_location.map { |c| [c]*cities.count(c) }
  #=> [["london", "london", "london"], ["new york"]]

Depending on your requirements, you might find it more useful to produce a hash:

Hash[desired_location.map { |c| [c, cities.count(c)] }]
  #=> {"london"=>3, "new york"=>1} 

2 Comments

thanks @cary, let me try and reply you back. seems it will work. from final hash, is it finally sorted. I mean, first element will always be top ?
As long as you are using Ruby v1.9+, the keys of the hash at the end are guaranteed to be ordered as they are in desired_location.
0

Another way:

cities = ['london', 'new york', 'london', 'london', 'washington']
puts cities.select{|city| cities.count(city) > 1}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.