Skip to content

Commit

Permalink
Add remove duplicates (keon#905)
Browse files Browse the repository at this point in the history
* Initial commit for remove duplicates

* Made changes to readme and added test case
  • Loading branch information
oDqnger authored Feb 5, 2024
1 parent e9c28af commit cad4754
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions algorithms/arrays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
from .two_sum import *
from .limit import *
from .n_sum import *
from .remove_duplicates import *
18 changes: 18 additions & 0 deletions algorithms/arrays/remove_duplicates.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit cad4754

Please sign in to comment.