diff --git a/algorithms/arrays/remove_duplicates.py b/algorithms/arrays/remove_duplicates.py index 1c0bc0a06..fc1fc9f6c 100644 --- a/algorithms/arrays/remove_duplicates.py +++ b/algorithms/arrays/remove_duplicates.py @@ -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 \ No newline at end of file + return new_array diff --git a/tests/test_array.py b/tests/test_array.py index f1ad11693..c9026644b 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -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, @@ -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): @@ -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):