forked from CoderAcademy-BRI/ruby-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25_remove_adjacent_duplicates.rb
More file actions
31 lines (24 loc) · 1.05 KB
/
Copy path25_remove_adjacent_duplicates.rb
File metadata and controls
31 lines (24 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Remove adjacent duplicates
# Sometimes we have input with adjacent duplicates, and we want to remove
# those adjacent duplicates, keeping the original input in order.
# Create a method, which takes a string argument and returns a string
# with duplicate adjacent elements removed, preserving the original
# order of characters and preserving original case. Your algorithm
# should be case sensitive, meaning that "Aa" should not be considered
# duplicate adjacent characters, like "aa" or "AA".
# *E.g.* remove_adjacent_duplicates("AAaAbbCCCcDDcDA") should return "AaAbCcDcDA"
# *E.g.* remove_adjacent_duplicates("abcABCabcABC") should return exactly the same string: "abcABCabcABC"
def remove_adjacent_duplicates(input)
characters = input.chars
i = 0
while i < characters.length
if characters[i] == characters[i + 1]
# Deleting at the current index gets us closer to the final output
# and has a similar effect to incrementing i
characters.delete_at(i)
else
i += 1
end
end
characters.join("")
end