From: Sean O'Halpin Date: 2007-10-29T03:47:28+09:00 Subject: Re: Duplicate elements in array On 10/28/07, Mohit Sindhwani wrote: > > Thanks Sean! Makes me feel quite nice about it. > > So, hashes are faster than arrays? > > Cheers, > Mohit. > 10/29/2007 | 2:13 AM. It depends what you're doing with them and how big they are. But in this instance, I changed your solution to use a hash because you were appending the duplicates to an array which resulted in adding an entry to that array every time you detected a duplicate. This didn't show up in your example because your data contained at most two instances of an item. If you change your example to: array = ["apple", "banana", "apple", "orange", "fat", "cow", "cow", "apple", "apple"] h = Hash.new duplicates = [] array.each {|item| if h.has_key?(item) then duplicates << item else h[item] = 0 #it doesn't matter what we store end } puts duplicates it outputs apple cow apple apple which is probably not what you want. Regards, Sean