Ruby

Remove Duplicates from a Ruby Array

An array is an incredible data structure that allows you to store a collection of elements. Each element in the array is assigned with a unique integer value that denotes its position in the array. Arrays are some of the most popular and versatile data structures in Ruby and other programming languages. You can use the arrays to store any type of data such as strings, integers, or even other arrays.

However, unlike other data structures that do not support duplicate values, arrays can hold more than one value of the same type and value. This can be a problem, especially when storing unique values.

This tutorial teaches us how to remove the duplicate elements from an array using various methods.

Using the Uniq Method in Ruby

In Ruby, we can use the uniq method to remove the duplicate elements from an input array. We can call this method on a given array. The method then returns a new array with the duplicate elements being removed.

The method removes the duplicate elements from the array in the order of appearance.

This method does not modify the original array. Instead, it creates a new array with unique elements, which we can assign to a new variable or used to update an existing variable.

Take the example array which is shown in the following:

arr = [1,2,3,1,3,3,1,2,3]

If we call the uniq method, it should return the previous array with the stripped duplicate elements.

irb(main):013:0> arr.uniq
=> [1, 2, 3]

No matter how many duplicate values are in the array, the method only returns a single value of type.

As mentioned, the uniq method does not modify the original array. Hence, we can store the resulting value into a new variable as shown in the following:

irb(main):014:0> new_arr = arr.uniq
=> [1, 2, 3]

To modify the original array, we can use the uniq! method. This method removes the duplicate values from the original array and modifies it to reflect the new array.

irb(main):015:0> arr.uniq!
=> [1, 2, 3]
irb(main):016:0> arr
=> [1, 2, 3]

As you can see, the value of the arr variable is permanently modified.

Fetching Unique Items by Class

The uniq method works by creating a hash out of the elements in the array. Hence, since the hash keys are unique, we can get the list of the keys and use them to create a new array.

However, we can pass a block to the method to filter out the elements by class. An example is demonstrated in the following:

irb(main):017:0> arr =  [1, '1', 2,'2' ,3, '3']

To sort out the duplicate values by class, run the following:

[cc lang="text" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]

[/cc]

We call the uniq method on arr with the &:class argument. This is known as “symbol to proc” conversion. It converts the symbol :class to a proc { |obj| obj.class }.

Calling the unique method with the proc as an argument returns a new array which contains only the unique elements based on the class of each element.

In this case, the method only preserves one 1, one “1”, and so on. But it keeps the first occurrence of the number and the first occurrence of the string.

This is because it uses the class method of the element. The class of the integers and the strings are different.

Conclusion

We discussed about using the uniq methods to remove the duplicate values from an input array. We also learned how to operate in place, allowing you to replace the original array and sort out the duplicate values based on the parent class.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list