forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuplicatesInArray.java
51 lines (46 loc) · 1.81 KB
/
DuplicatesInArray.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.rampatra.arrays;
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 8/21/15
*/
public class DuplicatesInArray {
/**
* Given an array of n elements which contains elements from 0 to n-1, with any of
* these numbers appearing any number of times. Find these repeating numbers in O(n)
* time complexity.
* <p/>
* For example, let n be 7 and array be {1, 2, 3, 1, 3, 6, 6}, the answer should be
* 1, 3 and 6.
* <p/>
* EXPLANATION:
* The algorithm is simple. We use index of the array to track repeating elements.
* Once we encounter a element lets say 2 then we make the element in 2nd index -ve just
* to mark that we have encountered 2. When we encounter 2 again and see that 2nd index
* is already -ve we conclude that 2 is repeated.
* <p/>
* Similar to {@link TwoRepeatingElements#findTwoRepeatingElements(int[])}.
*
* @param a
* @return
*/
public static int[] findDuplicatesInArray(int[] a) {
int[] duplicates = new int[a.length];
for (int i = 0, j = 0; i < a.length; i++) {
if (a[Math.abs(a[i])] >= 0) {
a[Math.abs(a[i])] = -a[Math.abs(a[i])];
} else {
duplicates[j++] = Math.abs(a[i]);
}
}
return duplicates;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(findDuplicatesInArray(new int[]{1, 1, 2, 3, 1, 3, 6, 6})));
// doesn't work if 0 is present in array (as -0 makes no sense but we can modify the algorithm to handle 0)
System.out.println(Arrays.toString(findDuplicatesInArray(new int[]{1, 0, 1, 2, 3, 1, 3, 6, 6})));
System.out.println(Arrays.toString(findDuplicatesInArray(new int[]{0, 0, 1, 2, 3, 1, 3, 6, 6})));
}
}