An example of whether there are duplicate values in a Java interpretation array
- 2020-04-01 03:16:14
- OfStack
An int[] array is shown here
public static void main(String[] args) {
int[] arry={1,10,5,8,11,100,99,10};
//A marker used to determine if there is a duplicate value
boolean flag=false;
for (int i = 0; i < arry.length; i++) {
int temp=arry[i];
int count=0;
for (int j = 0; j < arry.length; j++) {
int temp2=arry[j];
//If you have a duplicate value, then count plus 1
if(temp==temp2){
count++;
}
}
//Because once again in the middle you're going to compare yourself to yourself so here's count> = 2
if(count>=2){
flag=true;
}
}
if(flag){
System.out.println(" There are duplicate values!! ");
}else{
System.out.println(" No duplicate values exist!! ");
}
}