29

I have the following:

array = ["John", "Mike", "Bob", "Mike", "Bob"]

i want to get output:

[["Mike", "Mike"], ["Bob", "Bob"], ["John"]]
1
  • 3
    Welcome to StackOverflow! To get positive response here, it is best to show what you have tried. You can edit your question to show this. Commented Oct 4, 2012 at 10:34

1 Answer 1

55

Here is how to do that in Ruby.

array.group_by{ |x| x }.values
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to do this with proc shorthand?
@maxpleaner you can do names = ["John", "Mike", "Bob", "Mike", "Bob"]; names.group_by(&:itself).values

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.