File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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 >
                         You can’t perform that action at this time. 
           
                  
0 commit comments