Skip to content

Commit 0fbb93a

Browse files
Add find_all_duplicates.py under correct arrays folder
1 parent 44ba11e commit 0fbb93a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Author: Komal Srivastava
3+
Time Complexity: O(n)
4+
Space Complexity: O(1)
5+
"""
6+
7+
def find_duplicates(nums):
8+
res = []
9+
for num in nums:
10+
index = abs(num) - 1
11+
if nums[index] < 0:
12+
res.append(abs(num))
13+
else:
14+
nums[index] *= -1
15+
return res
16+
17+
if __name__ == "__main__":
18+
sample = [4,3,2,7,8,2,3,1]
19+
print("Duplicates:", find_duplicates(sample))

0 commit comments

Comments
 (0)