I count the freuqency for each number, and do reverse for the hashmap to map frequency to numbers, and stored the numbers that occured only once, and search the first occurence of this number.

Actually,we can have a more simple version (in Code-2), to further store the count for each frequency in freqCnt, and we only need to do dobule hashmap when searching again in nums.

Code-1:

class Solution {
public:
    int firstUniqueFreq(vector<int>& nums) {
        unordered_map<int, int> counts;
        for(const auto& num: nums) {
            ++counts[num];
        }

        unordered_map<int, vector<int>> numOfCnt;
        for(auto [num, cnt]: counts) {
            if(numOfCnt.count(cnt)) {
                numOfCnt[cnt].push_back(num);
            } else {
                numOfCnt[cnt] = {num};
            }
        }

        unordered_set<int> uniq;
        for(auto [cnt, v]: numOfCnt) {
            if(v.size() > 1) continue;
            uniq.insert(v[0]);
        }
        if(uniq.size() == 0) return -1;

        for(const auto& num: nums) {
            if(uniq.count(num)) return num;
        }
        return -1;
    }
};

Code-2: Simplified by ChatGPT

class Solution {
public:
    int firstUniqueFreq(vector<int>& nums) {
        unordered_map<int,int> cnt;
        for (int x : nums) ++cnt[x];

        unordered_map<int,int> freqCnt; // freq -> how many numbers have this freq
        for (auto &p : cnt) ++freqCnt[p.second];

        for (int x : nums) {
            if (freqCnt[cnt[x]] == 1) return x;
        }
        return -1;
    }
};