-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayfindDuplicate.py
34 lines (30 loc) · 1.04 KB
/
arrayfindDuplicate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# https://leetcode.com/problems/find-all-duplicates-in-an-array/submissions/
from typing import List
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
seen = set()
out = []
for num in nums:
if num not in seen:
seen.add(num)
else:
out.append(num)
return out
print(Solution().findDuplicates([4, 3, 2, 7, 8, 2, 3, 1]))
# [2, 3]
# 90% faster, but memory only less than 12% of other Python3 submissions
# all num in nums are positive. to use only O(1) space, use array itself as hashmap
# hashing function is simply abs(x) or abs(x)-1, and change value to negative once it has been seen.
# class Solution(object):
# def findDuplicates(self, nums):
# """
# :type nums: List[int]
# :rtype: List[int]
# """
# res = []
# for x in nums:
# if nums[abs(x)-1] < 0:
# res.append(abs(x))
# else:
# nums[abs(x)-1] *= -1
# return res