Finding duplicate element from 1-100 numbers JAVA



import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FindDuplicateNumber {

public static void main(String args[]){
int a[]={1,2,3,4,5,6,7,8,9,5};

System.out.println("Using arrays =>"+usingArrayDup(a));
}

// Finding duplicates using double arrays
private static int usingArrayDup(int[] a) {
//Finding duplicate number using two arrays
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]==a[j])
return a[i];
}
}
return 0;
}//Double arrays
}

Output:-

Using arrays =>5

2 comments :

  1. above the code does not valid to check more than one number. suppose i will add another one duplicate element your code does not valid

    ReplyDelete