每日力扣-哈希-重复N次的元素

90 阅读1分钟

在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次。

返回重复了 N 次的那个元素。---

class Solution {
public:
    unordered_map<int,int> hash;
    int repeatedNTimes(vector<int>& A) {
        for(int i = 0 ; i < A.size() ; i++)
        {
            hash[A[i]] ++;
        }
        for(const auto& x : hash)
        {
            if(x.second == A.size()/2) return x.first;
        }
        return 0;
    }
};