Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Най-често срещано число
- import java.util.HashMap;
- import java.util.Map;
- public class Zadachki {
- private static HashMap<Integer, Integer> map = new HashMap();
- public static void getValue() {
- int com = 0;
- int key = 0;
- for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
- if (com < entry.getValue()) {
- com = entry.getValue();
- key = entry.getKey();
- }
- }
- System.out.println("Most common "+key + " ==> " + com);
- }
- public static void HashMapFill(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- if (!map.containsKey(arr[i])) {
- map.put(arr[i], 1);
- } else if (map.containsKey(arr[i])) {
- int count = 1 + map.get(arr[i]);
- map.put(arr[i], count);
- }
- }
- }
- public static void main(String[] args) {
- int arr[] = {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3};
- HashMapFill(arr);
- System.out.println(map);
- getValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement