每日力扣-哈希-独一无二的出现次数

90 阅读1分钟

给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。

如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false。

解题思路:hash的作用之一就是筛出重复的数,连续筛出两次。

class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        unordered_map<int,int> hash;
        for(const auto& x : arr)
        {
            hash[x] ++;
        }
        unordered_set<int> s;
        for(const auto& i : hash)
        {
            s.insert(i.second);
        }
        if(s.size() == hash.size()) return true;
        return false;
    }
};