3

suppose I have an array [1,2,3,1,5,2]. Here 1 and 2 are the repeated elements. I want to get a new array [1,2].

How do I do that in ruby ??

1
  • @mihai, thanks, voted to close, now you can safely remove your answer. Commented Dec 10, 2011 at 19:06

2 Answers 2

12
arr = [1,2,3,1,5,2]
arr.group_by {|e| e}.map { |e| e[0] if e[1][1]}.compact

Pretty ugly... but does the job without an n+1 problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, perhaps clearer arr.group_by { |v| v }.select { |k, v| v.count > 1 }.keys
I actually started with that exact line, but took the .count out to do one less operation for every element.
4
arr = [1,2,3,1,5,2]
arr.select { |x| arr.count(x) > 1 } .uniq

A longer solution using reduce should be quicker.

arr.reduce [{}, []] do |(seen, out), cur|
  case seen[cur]
    when :seen then
      [seen.merge({cur => :added}), out << cur]
    when :added then
      [seen, out]
    else
     [seen.merge({cur => :seen}), out]
  end
end.last

4 Comments

I wrote something similar in the duplicated question: stackoverflow.com/a/8459032/188031. But I see some problems here: 1) you are using an array object to test inclussion, which is O(n), a hash or set would be more suitable. 2) you can safely remove the uniq. 3) Use arguments expansion instead of acc[0]/acc[1].
@tokland, thanks for the comment. 1) I've switched to Hash. 2) No, unfortunately not. See [1,1,1]. 3) Good point!
After the last change uniq isn't necessary any longer.
you are right! now I have to check my solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.