forked from VaibhavD74/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAllDuplicates.java
More file actions
36 lines (33 loc) · 1.02 KB
/
FindAllDuplicates.java
File metadata and controls
36 lines (33 loc) · 1.02 KB
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
package com.saikat;
import java.util.ArrayList;
import java.util.List;
public class FindAllDuplicates {
public static void main(String[] args) {
ArrayList<Integer> ans = new ArrayList<>();
int[] nums = {4,3,2,7,8,2,3,1};
System.out.println(findDisappearedNumbers(nums));
}
static List<Integer> findDisappearedNumbers(int[] nums) {
int i = 0;
while (i < nums.length){
int correctIndex = nums[i] - 1;
if (nums[i] != nums[correctIndex]){
swap(nums,i,correctIndex);
}else{
i++;
}
}
List<Integer> ans = new ArrayList<>();
for(int index = 0; index < nums.length; index++){
if(nums[index] != index + 1){
ans.add(index + 1);
}
}
return ans;
}
static void swap(int[] arr, int first, int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}