# 剑指offer面试题3-数组中重复的数字 java

public class duplicate {
//查找数组中重复的数字，加一个数组空间，判断
public static void duplicate(int[] numbers, int length, int[] duplication) {
boolean[] k = new boolean[length];

for (int i = 0; i < length; i++) {
if (k[numbers[i]] == true) {
duplication[0] = numbers[i];
System.out.println(numbers[i]);
}
k[numbers[i]] = true;
}
}

//查找数组中的重复数字，在原数组中判断，可以改变数组，将数字和数组下表相同的进行交换，然后判断
public static boolean duplication(int[] numbers, int length, int[] duplication) {
if (numbers == null || length <= 0) {
return false;
}
for (int i = 0; i < length; i++) {
if (numbers[i] < 0 || numbers[i] >= length) {
return false;
}
}
for (int i = 0; i < length; i++) {
while (numbers[i] != i) {
if (numbers[i] == numbers[numbers[i]]) {
duplication[0] = numbers[i];
return true;
}
int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}
}
return false;
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] numbers = {2, 3, 1, 0, 2, 5, 3};
int[] a = new int[2];
duplicate(numbers, 7, a);
System.out.print(duplication(numbers, 7, a));
}
}