Skip to content

Commit 31980e0

Browse files
authored
add second approach in find duplicate solution (CodingWallah#15)
1 parent 2084335 commit 31980e0

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

ArraysSolutions/Find All Duplicates in an Array.md

Lines changed: 31 additions & 1 deletion
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 (n)
46

57
#### Space Complexity - O(1)
@@ -39,7 +41,35 @@ 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>
45-
</div>
75+
</div>

0 commit comments

Comments
 (0)