From: Greg Willits Date: 2012-04-20T19:34:04+09:00 Subject: Re: finding duplicates in an array and its index number I do a lot of indexing of data. Usually they're multi-field records, and each data set will have multiple indexes. The technique I use is below. This indexes all elements of the array. So, then to find duplicates, you have to loop again to find array counts > 1. Anyway, it's a slightly different way to do it in the event an index of all entries would be useful for lookups. data = [1, 2, 3, 1, 3, 4, 6, 5, 4] dataIndex = {} data.each_with_index do |value, indx| index_key = value index_value = indx if !dataIndex.has_key?(index_key) dataIndex.store(index_key, []) end dataIndex[index_key].push(index_value) end p dataIndex dataIndex.each do |key, indxs| if indxs.count > 1 # do something puts "#{key} appears at #{indxs.join(', ')}" end end -- gw -- Posted via http://www.ruby-forum.com/.