-
Notifications
You must be signed in to change notification settings - Fork 43
/
find-all-duplicates-in-an-array.py
129 lines (107 loc) · 3.09 KB
/
find-all-duplicates-in-an-array.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
442. Find All Duplicates in an Array
Medium
Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n) time and uses only constant extra space.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]
Example 2:
Input: nums = [1,1,2]
Output: [1]
Example 3:
Input: nums = [1]
Output: []
Constraints:
n == nums.length
1 <= n <= 105
1 <= nums[i] <= n
Each element in nums appears once or twice.
"""
# V0
# IDEA : Counter
from collections import Counter
class Solution(object):
def findDuplicates(self, nums):
return [elem for elem, count in Counter(nums).items() if count == 2]
# V1
# http://bookshadow.com/weblog/2016/10/25/leetcode-find-all-duplicates-in-an-array/
# https://blog.csdn.net/fuxuemingzhu/article/details/79275549
# IDEA : FIND ALL ELEMENTS EXIST EXACTLY 2 TIMES IN THE GIVEN ARRAY
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans = []
for n in nums:
if nums[abs(n) - 1] < 0:
ans.append(abs(n))
else:
nums[abs(n) - 1] *= -1
return ans
# V1'
# http://bookshadow.com/weblog/2016/10/25/leetcode-find-all-duplicates-in-an-array/
# https://blog.csdn.net/fuxuemingzhu/article/details/79275549
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans = []
for i in range(len(nums)):
while nums[i] and nums[i] != i + 1:
n = nums[i]
if nums[i] == nums[n - 1]:
ans.append(n)
nums[i] = 0
else:
nums[i], nums[n - 1] = nums[n - 1], nums[i]
return ans
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = []
for i in nums:
if nums[abs(i)-1] < 0:
result.append(abs(i))
else:
nums[abs(i)-1] *= -1
return result
# Time: O(n)
# Space: O(1)
class Solution2(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = []
i = 0
while i < len(nums):
if nums[i] != nums[nums[i]-1]:
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
else:
i += 1
for i in range(len(nums)):
if i != nums[i]-1:
result.append(nums[i])
return result
# Time: O(n)
# Space: O(n), this doesn't satisfy the question
from collections import Counter
class Solution3(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
return [elem for elem, count in Counter(nums).items() if count == 2]