public class Array_Duplicate {
public static void main(String[] args){
int[] arr={2,4,2,2,6,3};
int count=0;
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]== arr[j]){
count++;
System.out.println(arr[i]+" Repeated "+count+" Times");
}
}
}
}
}
2 Repeated 1 Times
2 Repeated 2 Times
2 Repeated 3 Times
所需的输出
2 Repeated 3 times
我怎样才能做到这一点?
你是不是在你最初的方法使用任何类库,并接受它采用最类库和Java 8答案!太奇怪了!! 顺便说一句,使用'Map'将会绰绰有余,并且你的问题会变得非常初学者,看起来不像读他的书! – Yahya