0
0
RubyProgramBeginner · 2 min read

Ruby Program to Remove Duplicates from String

You can remove duplicates from a string in Ruby by using string.chars.uniq.join, which splits the string into characters, removes duplicates, and joins them back.
📋

Examples

Inputhello
Outputhelo
Inputbanana
Outputban
Input
Output
🧠

How to Think About It

To remove duplicates from a string, think of breaking the string into individual characters, then keeping only the first occurrence of each character while ignoring repeats, and finally joining these unique characters back into a string.
📐

Algorithm

1
Get the input string.
2
Split the string into individual characters.
3
Remove duplicate characters, keeping only the first occurrence.
4
Join the unique characters back into a single string.
5
Return or print the resulting string.
💻

Code

ruby
def remove_duplicates(str)
  str.chars.uniq.join
end

puts remove_duplicates("hello")
puts remove_duplicates("banana")
puts remove_duplicates("")
Output
helo ban
🔍

Dry Run

Let's trace the string "banana" through the code

1

Input string

"banana"

2

Split into characters

["b", "a", "n", "a", "n", "a"]

3

Remove duplicates with uniq

["b", "a", "n"]

4

Join characters back

"ban"

IterationCharacterUnique Characters So Far
1bb
2aba
3nban
4aban
5nban
6aban
💡

Why This Works

Step 1: Split string into characters

Using chars breaks the string into an array of single characters so we can work with each one.

Step 2: Remove duplicates

uniq keeps only the first occurrence of each character, removing any repeats.

Step 3: Join characters back

join combines the unique characters array back into a single string without duplicates.

🔄

Alternative Approaches

Using a loop and a set
ruby
def remove_duplicates(str)
  seen = {}
  result = ""
  str.each_char do |char|
    unless seen[char]
      result += char
      seen[char] = true
    end
  end
  result
end

puts remove_duplicates("hello")
This method manually tracks seen characters and builds the result, which is more explicit but longer.
Using regular expression
ruby
def remove_duplicates(str)
  str.gsub(/(.)(?=.*\1)/, '')
end

puts remove_duplicates("banana")
This uses a regex to remove characters that appear later, but can be less readable for beginners.

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

Time Complexity

The method loops through each character once to create an array and then removes duplicates, so it runs in linear time relative to string length.

Space Complexity

Extra space is used to store the array of characters and the unique characters, so space grows linearly with input size.

Which Approach is Fastest?

Using chars.uniq.join is both concise and efficient, while manual loops add overhead and regex can be slower and harder to read.

ApproachTimeSpaceBest For
chars.uniq.joinO(n)O(n)Simple and readable code
Loop with setO(n)O(n)Explicit control, easy to customize
RegexO(n^2)O(n)Compact but less readable, slower on large strings
💡
Use string.chars.uniq.join for a quick and readable way to remove duplicates.
⚠️
Beginners often try to remove duplicates without splitting the string into characters first, causing errors.