public class Test{
public static void main(String[] args) {
try {
Scanner sc=new Scanner(System.in);
System.out.print("ENTER INPUT ARRAY SIZE : ");
int inputArraySize = sc.nextInt();
System.out.println("ENTER "+inputArraySize+" ARRAY ELEMENTS :");
int[] inputArr=new int[inputArraySize];
for(int i=0;i<inputArraySize;i++){
inputArr[i] = sc.nextInt();
}
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
for(int i=0;i<inputArr.length;i++){
if(map.containsKey(inputArr[i])){
map.put(inputArr[i], map.get(inputArr[i])+1);
}
else{
map.put(inputArr[i], 1);
}
}
int max = Collections.max(map.values());
List<Integer> keys = new ArrayList<Integer>();
for(Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() == max){
keys.add(entry.getKey());
}
}
System.out.println("OUTPUT\nElement\tCount");
System.out.println(Collections.min(keys)+"\t"+max);
} catch (Exception e) {
System.err.println("** ENTER NUMERIC DIGITS ONLY **");
}
}
}

No comments:
Post a Comment