LC 1207. Unique Number of Occurrences

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
public:
    bool uniqueOccurrences(vector<int>& arr) {
        sort(arr.begin(),arr.end());
        int n = arr.size(),cnt = 1;
        if(n==1)
            return true;
        vector<int> occurance_cnt(n+1,0);
        for(int i=0;i<n-1;i++){
            if(arr[i] == arr[i+1])
                cnt++;
            else if(occurance_cnt[cnt])
                return false;
            else{
                occurance_cnt[cnt] = 1;
                cnt = 1;
            }
        }
        if(occurance_cnt[cnt])
                return false;
        else{
            occurance_cnt[cnt] = 1;
            cnt = 1;
        }
        return true;
    }
};


Dicussion Forum