0
0
RubyProgramBeginner · 2 min read

Ruby Program to Remove Duplicates from an Array

In Ruby, you can remove duplicates from an array using the uniq method like this: array.uniq which returns a new array without duplicates.
📋

Examples

Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 2, 3, 4, 5]
Input["apple", "banana", "apple", "orange"]
Output["apple", "banana", "orange"]
Input[]
Output[]
🧠

How to Think About It

To remove duplicates, think of scanning the list and keeping only the first time you see each item. Ruby's uniq method does this by creating a new list with only unique elements, preserving their order.
📐

Algorithm

1
Take the input array.
2
Check each element in order.
3
Keep the element if it has not appeared before.
4
Skip the element if it is a duplicate.
5
Return the new array with only unique elements.
💻

Code

ruby
array = [1, 2, 2, 3, 4, 4, 5]
unique_array = array.uniq
puts unique_array.inspect
Output
[1, 2, 3, 4, 5]
🔍

Dry Run

Let's trace the array [1, 2, 2, 3, 4, 4, 5] through the code using the uniq method.

1

Start with original array

[1, 2, 2, 3, 4, 4, 5]

2

Check each element and keep unique ones

Keep 1, keep 2, skip second 2, keep 3, keep 4, skip second 4, keep 5

3

Resulting array after removing duplicates

[1, 2, 3, 4, 5]

ElementActionCurrent Unique Array
1Keep[1]
2Keep[1, 2]
2Skip[1, 2]
3Keep[1, 2, 3]
4Keep[1, 2, 3, 4]
4Skip[1, 2, 3, 4]
5Keep[1, 2, 3, 4, 5]
💡

Why This Works

Step 1: Using uniq method

The uniq method scans the array and builds a new array with only the first occurrence of each element.

Step 2: Preserves order

uniq keeps the original order of elements, so the first time an element appears is kept.

Step 3: Returns new array

It returns a new array without duplicates, leaving the original array unchanged.

🔄

Alternative Approaches

Using a hash to track seen elements
ruby
array = [1, 2, 2, 3, 4, 4, 5]
seen = {}
unique_array = array.select { |e| !seen[e] && (seen[e] = true) }
puts unique_array.inspect
This method manually tracks seen elements and can be useful if you want to customize duplicate handling.
Using Array#uniq! to modify in place
ruby
array = [1, 2, 2, 3, 4, 4, 5]
array.uniq!
puts array.inspect
This modifies the original array instead of creating a new one, saving memory but changing the input.

Complexity: O(n) time, O(n) space

Time Complexity

The uniq method scans each element once, so it runs in linear time relative to the array size.

Space Complexity

It creates a new array and a set of seen elements, so it uses extra space proportional to the number of unique elements.

Which Approach is Fastest?

Using uniq is the fastest and simplest for most cases; manual hash tracking is similar but more verbose; uniq! saves space by modifying in place.

ApproachTimeSpaceBest For
array.uniqO(n)O(n)Simple, non-destructive duplicate removal
Manual hash trackingO(n)O(n)Custom duplicate logic
array.uniq!O(n)O(1) extraIn-place modification to save memory
💡
Use array.uniq to quickly get a new array without duplicates while keeping the original array intact.
⚠️
Beginners often try to remove duplicates by manually looping without using uniq, which is less efficient and more error-prone.