Skip to content

Latest commit

 

History

History
82 lines (72 loc) · 2.11 KB

1207. Unique Number of Occurrences.md

File metadata and controls

82 lines (72 loc) · 2.11 KB

leetcode-cn Daily Challenge on October 28th, 2020.


Difficulty : Easy

Related Topics : HashTable


Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.

Example 1:

Input: arr = [1,2,2,1,1,3]
Output: true
Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.

Example 2:

Input: arr = [1,2]
Output: false

Example 3:

Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output: true

Constraints:

  • 1 <= arr.length <= 1000
  • -1000 <= arr[i] <= 1000

Solution

  • mine
    • Java
      • use map to collection the value. Runtime: 2 ms, faster than 88.01%, Memory Usage: 35.6 MB, less than 100.00%
        public boolean uniqueOccurrences(int[] arr) {
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < arr.length; i++) {
                int t = arr[i];
                if (map.containsKey(t)) {
                    map.put(t, map.get(t) + 1);
                } else {
                    map.put(t, 1);
                }
            }
            List<Integer> list = new ArrayList<>();
            Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                int v = iterator.next().getValue();
                if (list.contains(v)) {
                    return false;
                }else{
                    list.add(v);
                }
            }
            return true;
        } 
        

  • the most votes
  • Runtime: 2 ms, faster than 88.01%, Memory Usage: 36 MB, less than 100.00%
    public boolean uniqueOccurrences(int[] arr) {
        Map<Integer, Integer> count = new HashMap<>();
        for (int a : arr)
            count.put(a, 1 + count.getOrDefault(a, 0));
        return count.size() == new HashSet<>(count.values()).size();
    }