Problem statement : In an array 1-100, many numbers are duplicates, how do you find it?
we can loop over array and put the values in a map to find out duplicate numbers with the number of time they repeat them selves.
int[] numbers = new int [100]; |
The solution I proposed gives you the freedom of printing you data in several way or store the data in a manner which is most easy to retrieve. ie create a map for each number and save them against the key.
public class manyDuplicates {
public static void main(String[] args)
{
HashSet hset = new HashSet();
int []arr={1,1,2,3,4,4};
boolean flag;
for(int i=0;i<arr.length;i++)
{
flag=hset.add(arr[i]);
if(!flag)
System.out.println(arr[i]);
}
}
}
LikeLiked by 1 person
Its great !!!!
LikeLike