-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN51DuplicatedNbInArray.java
More file actions
54 lines (54 loc) · 2.06 KB
/
N51DuplicatedNbInArray.java
File metadata and controls
54 lines (54 loc) · 2.06 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* https://github.com/zhedahht/ChineseCodingInterviewAppendix/blob/master/DuplicationInArray/FindDuplication.cpp
* Question Description:
* An array contains n numbers ranging from 0 to n-1.
* There are some numbers duplicated in the array. It is not clear how many numbers are duplicated or
* how many times a number gets duplicated. How do you find a duplicated number in the array?
* For example, if an array of length 7 contains the numbers {2, 3, 1, 0, 2, 5, 3},
* the implemented function (or method) should return either 2 or 3
*/
public class N51DuplicatedNbInArray {
public static void main(String[] args) {
int[] array = {2, 3, 1, 0, 2, 5, 3};
findDuplicatedNb(array);
findDuplicate(array);
}
public static int findDuplicatedNb(int[] array) {
if(array == null || array.length == 0) {
throw new IllegalArgumentException("invalid array");
}
HashMap<Integer, Integer> appears = new HashMap<Integer, Integer>();
for(int i=0; i<array.length; i++) {
if(appears.get(array[i]) == null) {
appears.put(array[i], 1);
}
else {
System.out.println("Found duplicated nb:"+array[i]);
return array[i]; //find the duplicated number
}
}
return -1; // not found return -1, different with numbers [0,n-1]
}
public static int findDuplicate(int[] numbers) {
if(numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("invalid input");
}
for(int i=0; i<numbers.length; i++) { // range check
if(numbers[i] < 0 || numbers[i] > numbers.length-1) {
return -1;
}
}
for(int i=0; i<numbers.length; i++) {
while(numbers[i] != i) {
if(numbers[i] == numbers[numbers[i]]) {
System.out.println("Found duplicated nb:"+numbers[i]);
return numbers[i];
}
// swap numbers[i] with numbers[numbers[i]]
int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
} // end while
}// end for
return -1; // not found return -1, different with numbers [0,n-1]
}
}