Skip to content

Commit 4f7551e

Browse files
authored
Merge branch 'main' into patch-3
2 parents f99b1e2 + 31980e0 commit 4f7551e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

ArraysSolutions/Find All Duplicates in an Array.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Solution of Find All Duplicates in an Array
22

3+
### First Approach
4+
35
#### Time Complexity - O (nlogn)
46

57
#### Space Complexity - O(1)
@@ -39,6 +41,34 @@ return list;
3941
}
4042

4143
```
44+
### Second Approach
45+
46+
#### Time Complexity - O (n)
47+
48+
#### Space Complexity - O(1)
49+
50+
```java
51+
class Solution {
52+
public List<Integer> findDuplicates(int[] nums) {
53+
List <Integer> list = new ArrayList <Integer>();
54+
int n = nums.length;
55+
int index;
56+
for (int i = 0 ; i < n; i++){
57+
int num = nums[i];
58+
index = Math.abs(num) - 1 ;
59+
if ( nums[index] < 0){
60+
list.add(Math.abs(nums[i]));
61+
}
62+
else{
63+
nums[index] = nums[index] * -1;
64+
}
65+
}
66+
return list;
67+
}
68+
}
69+
70+
```
71+
4272

4373
<div align="right">
4474
Contributed By <a href="https://github.com/rath23"> Muneer Ahmad</a>

0 commit comments

Comments
 (0)