Skip to content

Commit cad4754

Browse files
authored
Add remove duplicates (#905)
* Initial commit for remove duplicates * Made changes to readme and added test case
1 parent e9c28af commit cad4754

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ If you want to uninstall algorithms, it is as simple as:
6363
- [merge_intervals](algorithms/arrays/merge_intervals.py)
6464
- [missing_ranges](algorithms/arrays/missing_ranges.py)
6565
- [plus_one](algorithms/arrays/plus_one.py)
66+
- [remove_duplicates](algorithms/arrays/remove_duplicates.py)
6667
- [rotate](algorithms/arrays/rotate.py)
6768
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
6869
- [three_sum](algorithms/arrays/three_sum.py)

algorithms/arrays/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
from .two_sum import *
1717
from .limit import *
1818
from .n_sum import *
19+
from .remove_duplicates import *
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
This algorithm removes any duplicates from an array and returns a new array with those duplicates
3+
removed.
4+
5+
For example:
6+
7+
Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True]
8+
Output: [1, 2, 3, 4, 'hey', 'hello']
9+
"""
10+
11+
def remove_duplicates(array):
12+
new_array = []
13+
14+
for item in array:
15+
if item not in new_array:
16+
new_array.append(item)
17+
18+
return new_array

tests/test_array.py

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
missing_ranges,
1010
move_zeros,
1111
plus_one_v1, plus_one_v2, plus_one_v3,
12+
remove_duplicates
1213
rotate_v1, rotate_v2, rotate_v3,
1314
summarize_ranges,
1415
three_sum,
@@ -298,6 +299,15 @@ def test_plus_one_v3(self):
298299
self.assertListEqual(plus_one_v3([9, 9, 9, 9]),
299300
[1, 0, 0, 0, 0])
300301

302+
class TestRemoveDuplicate(unittest.TestCase):
303+
304+
def test_remove_duplicates(self):
305+
self.assertListEqual(remove_duplicates([1,1,1,2,2,2,3,3,4,4,5,6,7,7,7,8,8,9,10,10]))
306+
self.assertListEqual(remove_duplicates(["hey", "hello", "hello", "car", "house", "house"]))
307+
self.assertListEqual(remove_duplicates([True, True, False, True, False, None, None]))
308+
self.assertListEqual(remove_duplicates([1,1,"hello", "hello", True, False, False]))
309+
self.assertListEqual(remove_duplicates([1, "hello", True, False]))
310+
301311

302312
class TestRotateArray(unittest.TestCase):
303313

0 commit comments

Comments
 (0)