sundeepblue
4/12/2014 - 2:07 PM

Find the first distinct element in an array

Find the first distinct element in an array

int find_first_distinct_element(vector<int>& A) {
    if(A.empty()) return -1;
    unordered_map<int, int> mp
    for(auto i : A)
        mp[i]++;
    for(auto i : A)
        if(mp[i] == 1) 
            return i;
    return -1;
}