DEV Community

Cover image for How to Remove Duplicates from Arrays
Sean
Sean

Posted on

2

How to Remove Duplicates from Arrays

Hmm...

Okay, you want to remove duplicates from your array. As a beginner this might prove a difficult task. Luckily, Ruby has created more than one way to do this, so you don't spend your time writing a function for it. Let's get into it.

Duplicates, Duplicates, Duplicates...

So, what are these methods anyway? Well, since you asked so nicely you can use the unique method (.uniq), the difference operator(-), the union operator(|), and the intersection operator(&), your own short-hand method(which we'll get into later), and to_set.

Let's Start From the Start, Syntax

Let's take a look at the syntax for each method.

#uniq syntax(unintended pun)
arr.uniq
#union operator syntax
arr | arr
#intersection operator syntax
arr & arr
#difference operator
arr - arr
#and finally the to set syntax
arr.to_set
#to turn it back to an array
arr.to_set.to_a

Let's see how they work

You better work!

Uniq

#Defined the array
arr = [2, 3, 4, 5, 6, 5, 7, 7, 77, 96, 3]
#Puts the original array and the duplicateless array
puts "#{arr}" #=> [2, 3, 4, 5, 6, 5, 7, 7, 77, 96, 3]
#Using the method uniq without a block checks if an item is overall the exact same as the other
puts "\n#{arr.uniq}" #=> [2, 3, 4, 5, 6, 7, 77, 96]
#redefines arr
arr = ["3", "34242", "fsa5", "4444", "food"]
#With a block it checks for duplicates that fit the condition or return the same value in a specific instance
#puts arr, and arr.uniq with and without a block
puts "\n#{arr}" #=> ["3", "34242", "fsa5", "4444", "food"]
#here putting uniq doesn't do anything because all the items aren't entirely the same
puts "\n#{arr.uniq}\n\n" #=> ["3", "34242", "fsa5", "4444", "food"]
#here the block specifies what to look for, the item's length, so the last two items are removed
puts arr.uniq {|x| x.length}.to_s #=> ["3", "34242", "fsa5"]
#and finally to permanently change the array
arr.uniq!
view raw uniq uses.rb hosted with ❤ by GitHub

The operators

These three operators have different uses. The Difference operator returns items from the first array that DON'T appear in the second and vice versa. The Union operator can be used to return one of each item in both arrays, which guarantees that there will not be any duplicates. The Intersection operator returns items that appear in both arrays, also without duplicates.
After all I've said you're probably thinking,

but to your surprise there actually is a reason that I'm telling you this. Here, let me show you.

#here's our list
arr = [1, 2, 3, 4, 5, 6, 6, 67]
#here's the union operator
arr | arr #=> [1, 2, 3, 4, 5, 6, 67]
#if we have a second array we'll get this instead
arr2 = [3, 5, 6, 7, 5, 8, 0]
arr | arr2 #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 67]
#here's the intersection operator
arr & arr #=> [1, 2, 3, 4, 5, 6, 67]
"see how it gives us the same outcome as the union operator
that's because the union operator returns one of each item in each list
and the intersection operator returns one of each item that appear in both lists
so if the same list is on both sides of either operator, the output will be the same
but if it's not then it is entirely different
like so"
arr & arr2 #=> [3, 5, 6]
"why did this happen? it's because those are the only numbers that are in arr and arr2
so essentially the intersection operator will return duplicates from different array
making it easy to snuff them out"
#here's the difference operator
arr - arr #=> []
#why'd this happen? because the diference operator returns the items from the first and second array that don't appear in the other
#so if the same array is on both sides of the operator it WILL return an EMPTY array
#so don't use it unless you want to determine if both arrays contain the same items
#with a different array
arr - arr2 #=> [0, 1, 2, 4, 7, 8, 67]

Don't worry you're almost to_set!

The to_set method is a lot simpler than the previous methods. You have likely already learned what a set is, but for those who haven't a quick recap.
Here we go again!

Bruuuh

Sets

Sets are unordered lists that can't have duplicates. So the to_set method creates an instance of the array to a set. Making all the duplicates disappear. So you can assign the variable to the duplicate free list.

to_set usage

Let's see!

#here's our array
arr = [4, 5, 6, 6]
arr.to_set #=> {4, 5, 6}
arr #=> [4, 5, 6, 6]
#to make the change permanent reassgin the varable
arr = arr.to_set
arr #=> {4, 5, 6}
#and to return it to an array
arr = arr.to_a
#you can do it as a one liner to be concise
arr = arr.to_set.to_a
view raw to_set usage.rb hosted with ❤ by GitHub

Finally, what I've been waiting to write!

FINALLY!!!!!

FINALLY!

It's not the end yet, but now we get to see a all the ways mentioned before and my own custom method in action.


Mine is not as good, for it to work it has to return the array sorted. Anyway, all this reading is probably boring you so I hope you liked this post, if you have any questions put them in the comments.

BYE ;)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay