在对整数数组进行排序后,"arr"
我尝试识别数组中的重复项。它对数组的一部分执行此操作,但不会识别整个数组中的重复项。我很感激帮助。这是我的代码:
import java.util.*;
public class Ex24 {
public static void main(String[] args) {
int i, n = 100;
Integer[] arr = Collections.nCopies(n, 0).toArray(new Integer[0]);
// int[] array = {0};
for (int j = 0; j < n ; j++){
arr[j] = (int) (Math.random() * 365 + 1);
// System.out.print(j + " " + arr[j] + "\n");
}
Arrays.sort(arr); //sort the arr
// String arrq = (Arrays.toString(arr));
System.out.println("\n");
Duplicate(arr, n);
}
static void Duplicate(Integer[] arr, int n) {
int j = 0;
for (int i = 0; i < n-1 ; i++) {
if (arr[i] != arr[i+1]) {
System.out.println(" at i (" + i + ") print arr[i] " + arr[i]);
}
else {
j = j +1;
System.out.println(" j = " + j + " at i (" + i + ") arr[i] is double at date " + arr[i]);
}
}
}
}