Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions algorithms/arrays/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@

For example:

Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True]
Output: [1, 2, 3, 4, 'hey', 'hello']
Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True, False]
Output: [1, 2, 3, 4, 'hey', 'hello', True, False]
"""


def remove_duplicates(array):
new_array = []
"""Removes duplicates from the input array while preserving the order of the first occurrence of each distinct item.

Args:
array (list): The input array containing elements with potential duplicates.

Returns:
list: A new array with duplicates removed, maintaining the order of the first occurrence of each item.

Examples:
>>> remove_duplicates([1, 1, 1, 2, 2, 3, 4, 4, "hey", "hey", "hello", True, True, False])
[1, 2, 3, 4, 'hey', 'hello', True, False]
"""
new_array = [] # Preserve order of first distinct item
seen = set() # Track the unique items

for item in array:
if item not in new_array:
if all([item is not elem for elem in seen]):
new_array.append(item)
seen.add(item)

return new_array
return new_array
27 changes: 17 additions & 10 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
missing_ranges,
move_zeros,
plus_one_v1, plus_one_v2, plus_one_v3,
remove_duplicates
remove_duplicates,
rotate_v1, rotate_v2, rotate_v3,
summarize_ranges,
three_sum,
Expand Down Expand Up @@ -299,14 +299,21 @@ def test_plus_one_v3(self):
self.assertListEqual(plus_one_v3([9, 9, 9, 9]),
[1, 0, 0, 0, 0])

class TestRemoveDuplicate(unittest.TestCase):

class TestRemoveDuplicate(unittest.TestCase):
def test_remove_duplicates(self):
self.assertListEqual(remove_duplicates([1,1,1,2,2,2,3,3,4,4,5,6,7,7,7,8,8,9,10,10]))
self.assertListEqual(remove_duplicates(["hey", "hello", "hello", "car", "house", "house"]))
self.assertListEqual(remove_duplicates([True, True, False, True, False, None, None]))
self.assertListEqual(remove_duplicates([1,1,"hello", "hello", True, False, False]))
self.assertListEqual(remove_duplicates([1, "hello", True, False]))
self.assertListEqual(remove_duplicates([1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 7, 8, 8, 9, 10, 10]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
self.assertListEqual(remove_duplicates(["hey", "hello", "hello", "car", "house", "house"]),
["hey", "hello", "car", "house"])
self.assertListEqual(remove_duplicates([True, True, False, True, False, None, None]),
[True, False, None])
self.assertListEqual(remove_duplicates([1, 1, "hello", "hello", True, False, False]),
[1, "hello", True, False])
self.assertListEqual(remove_duplicates([1, "hello", True, False]),
[1, "hello", True, False])
self.assertListEqual(remove_duplicates([False, True, False, True]),
[False, True])


class TestRotateArray(unittest.TestCase):
Expand Down Expand Up @@ -347,11 +354,11 @@ class TestSummaryRanges(unittest.TestCase):
def test_summarize_ranges(self):

self.assertListEqual(summarize_ranges([0, 1, 2, 4, 5, 7]),
[(0, 2), (4, 5), (7, 7)])
['0-2', '4-5', '7'])
self.assertListEqual(summarize_ranges([-5, -4, -3, 1, 2, 4, 5, 6]),
[(-5, -3), (1, 2), (4, 6)])
['-5--3', '1-2', '4-6'])
self.assertListEqual(summarize_ranges([-2, -1, 0, 1, 2]),
[(-2, 2)])
['-2-2'])


class TestThreeSum(unittest.TestCase):
Expand Down