File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
0442 Find All Duplicates in an Array Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ # 0442. Find All Duplicates in an Array
2+
3+ - Difficulty: medium
4+ - Link: https://leetcode.com/problems/find-all-duplicates-in-an-array
5+ - Topics: Array-String
6+
7+ # Clarification
8+
9+ 1 . Check the inputs and outputs
10+ - INPUT: List[ int]
11+ - OUTPUT: List[ int]
12+ 2 . Check the main goal
13+ - return the duplicate numbers
14+ - time complexity $O(n)$
15+ - space complexity $O(1)$
16+
17+ # Naive Solution
18+
19+ ### Thought Process
20+
21+ 1 . iterate the array
22+ 1 . consider the element as the index
23+ 2 . find the corresponding element
24+ 1 . is already negative → duplicate
25+ 2 . times it with negative
26+ - Implement
27+
28+ ``` python
29+ class Solution :
30+ def findDuplicates (self , nums : List[int ]) -> List[int ]:
31+ """
32+ 1. iterate the array
33+ a. consider the element as the index
34+ b. find the corresponding element
35+ i. is already negative → duplicate
36+ ii. times it with negative
37+ """
38+ result = []
39+ for num in nums:
40+ absNum = abs (num)
41+ if nums[absNum - 1 ] < 0 :
42+ result.append(absNum)
43+ nums[absNum - 1 ] *= (- 1 )
44+ return result
45+ ```
46+
47+
48+ # ## Complexity
49+
50+ - Time complexity: $ O(n)$
51+
52+ 
53+
54+ - Space complexity:$ O(1 )$
You can’t perform that action at this time.
0 commit comments