β Most occurring elements(Contest)
Most occurring elements easy asked in interviews by 1 company Time Limit: 2 sec Memory Limit: 128000 kB
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for(int i=0; i<arr.length; i++){
arr[i] = sc.nextInt();
}
Map<Integer, Integer> map = new HashMap<>();
for (int i : arr) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((o1, o2) -> {
if (o2.getValue().equals(o1.getValue())) {
return o1.getKey().compareTo(o2.getKey());
}
return o2.getValue().compareTo(o1.getValue());
});
int count = 0;
for (Map.Entry<Integer, Integer> entry : list) {
System.out.print(entry.getKey() + " ");
count++;
if (count == 3) {
break;
}
}
while (count < 3) {
System.out.print(-1 + " ");
count++;
}
System.out.println();
}
}
```Last updated