Array Remove DuplicatesUse the uniq method to remove duplicate elements from arrays with minimal code.
This page was last reviewed on Mar 21, 2023.
Remove duplicates. In 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.
Method info. 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.
First example. 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.
So When we call "uniq!" we do not assign another array to it. The methods imply changes the "values" array, which we reuse.
Next We create an array of strings. This one has three string values equalling "dog."
Finally We call uniq and assign another array to it result. Two of the "dog" values were eliminated—they must have run away.
# 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 duplicates. This 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.
Note This method is efficient because it uses a hash to search for duplicates. But avoiding duplicates in the first place would be faster.
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
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 21, 2023 (grammar).
© 2007-2024 Sam Allen.