From cad4754bc71742c2d6fcbd3b92ae74834d359844 Mon Sep 17 00:00:00 2001 From: oDqnger <103481200+oDqnger@users.noreply.github.com> Date: Mon, 5 Feb 2024 23:03:25 +0000 Subject: [PATCH] Add remove duplicates (#905) * Initial commit for remove duplicates * Made changes to readme and added test case --- README.md | 1 + algorithms/arrays/__init__.py | 1 + algorithms/arrays/remove_duplicates.py | 18 ++++++++++++++++++ tests/test_array.py | 10 ++++++++++ 4 files changed, 30 insertions(+) create mode 100644 algorithms/arrays/remove_duplicates.py diff --git a/README.md b/README.md index 0ec23733f..65caeb5ca 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ If you want to uninstall algorithms, it is as simple as: - [merge_intervals](algorithms/arrays/merge_intervals.py) - [missing_ranges](algorithms/arrays/missing_ranges.py) - [plus_one](algorithms/arrays/plus_one.py) + - [remove_duplicates](algorithms/arrays/remove_duplicates.py) - [rotate](algorithms/arrays/rotate.py) - [summarize_ranges](algorithms/arrays/summarize_ranges.py) - [three_sum](algorithms/arrays/three_sum.py) diff --git a/algorithms/arrays/__init__.py b/algorithms/arrays/__init__.py index 9670db750..2632ca1c7 100644 --- a/algorithms/arrays/__init__.py +++ b/algorithms/arrays/__init__.py @@ -16,3 +16,4 @@ from .two_sum import * from .limit import * from .n_sum import * +from .remove_duplicates import * \ No newline at end of file diff --git a/algorithms/arrays/remove_duplicates.py b/algorithms/arrays/remove_duplicates.py new file mode 100644 index 000000000..1c0bc0a06 --- /dev/null +++ b/algorithms/arrays/remove_duplicates.py @@ -0,0 +1,18 @@ +""" +This algorithm removes any duplicates from an array and returns a new array with those duplicates +removed. + +For example: + +Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True] +Output: [1, 2, 3, 4, 'hey', 'hello'] +""" + +def remove_duplicates(array): + new_array = [] + + for item in array: + if item not in new_array: + new_array.append(item) + + return new_array \ No newline at end of file diff --git a/tests/test_array.py b/tests/test_array.py index eaabfc710..f1ad11693 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -9,6 +9,7 @@ missing_ranges, move_zeros, plus_one_v1, plus_one_v2, plus_one_v3, + remove_duplicates rotate_v1, rotate_v2, rotate_v3, summarize_ranges, three_sum, @@ -298,6 +299,15 @@ def test_plus_one_v3(self): self.assertListEqual(plus_one_v3([9, 9, 9, 9]), [1, 0, 0, 0, 0]) +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])) + class TestRotateArray(unittest.TestCase):