Coding Test/HackerRank

[HackerRank] Lonely Integer c++

owls 2022. 7. 20. 09:38
728x90
  • Problem

Given an array of integers, where all elements but one occur twice, find the unique element.

  • Example

a = [1,2,3,4,3,2,1]

The unique element is 4.

  • Constraints

1<=n<=100

It is guaranteed that n is an odd number and that there is one unique element.

0<=a[i] <= 100, where 0 <= i < n.

  • Solutions
int lonelyinteger(vector<int> a) {
    unordered_map<int, int> nHash;
    for(auto &it : a){
        nHash[it]++;
    }    
    int result = 0;
    for(auto &it : nHash){
        if(it.second == 1) result = it.first;
    }
    return result;
}

728x90