Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Scanner;
- public class MostFrequent {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
- int maxValue = 0;
- int maxKey = 0;
- for (int i = 0; i < n; i++) {
- int temporary = scanner.nextInt();
- if (map.containsKey(temporary)){
- map.put(temporary, map.get(temporary) + 1);
- }
- else{
- map.put(temporary, 1);
- }
- Integer key = temporary;
- Integer value = map.get(temporary);
- if(maxValue < value){
- maxKey = key;
- maxValue = value;
- }
- }
- System.out.println(maxKey + " (" + maxValue + " times)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement