Finding duplicate element from 1-100 numbers using java.
Using Arrays, Collection Sort and HashSet.
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));
System.out.println("Using Collection Array Sort"+usingCollArraySortDup(a));
System.out.println("Using SortedSet Array Sort"+usingCollSortedSetDup(a));
}
//Finding duplicate elements using Hashset
private static int usingCollSortedSetDup(int[] a) {
Set<Integer> setsort=new HashSet<Integer>();
for(int i=0;i<a.length;i++)
if(setsort.contains(a[i]))
return a[i];
else
setsort.add(a[i]);
return 0;
}
//Finding duplicate numbers using collection sorting
private static int usingCollArraySortDup(int[] b) {
Arrays.sort(b);
for(int i=1;i<b.length;i++)
if(b[i]==b[i-1])
return(b[i]);
return 0;
}
// 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
}
sir ji agar int[]a={1,1,2,3,4,}
ReplyDeletede to duplicate value nahi find krta hai
kewal one duplicate value find krta hai