Remove
duplicatesIn an array, any element value may occur. But often we want just unique, non-duplicate elements. We invoke the uniq method to correct this problem.
Uniq and "uniq!" remove all except the first element with a value. But often programs are more efficient (and clearer) when values are kept in a hash, which eliminates dupes.
We create 2 arrays. We first create an array containing integers. The array has two duplicated numbers: a 2 and a 3. We invoke the "uniq!" method, which operates in-place.
string
values equalling "dog."# An input array. values = Array[4, 1, 2, 2, 3, 3] print values, "\n" # Convert the array with uniq. values.uniq! print values, "\n" # A string array. names = Array["cat", "dog", "dog", "dog", "mouse"] print names, "\n" # Create array copy with only unique values. unique = names.uniq print unique, "\n"[4, 1, 2, 2, 3, 3] [4, 1, 2, 3] ["cat", "dog", "dog", "dog", "mouse"] ["cat", "dog", "mouse"]
Find
duplicatesThis method uses the each iterator over on an array, and uses a hash to record which elements have been encountered. It then reports duplicates as it locates them.
def find_duplicates(elements) encountered = {} # Examine all elements in the array. elements.each do |e| # If the element is in the hash, it is a duplicate. if encountered[e] puts "Dupe exists for: " << e else # Record that the element was encountered. encountered[e] = 1 end end end # Use the example method. elements = ["bird", "dog", "bird", "cat"] find_duplicates(elements)Dupe exists for: bird
In Ruby, we find many convenience methods—uniq is one. In many languages, such functions must written from scratch. Ruby makes this unnecessary.
With a hash, we can eliminate duplicates automatically—this makes more sense for many programs. Ordering is lost in this way. Uniq meanwhile retains ordering.