Find an index of an array such that its value occurs at more than half of indices in the array.
Solution
#include <unordered_map>int solution(vector<int> &A) { // write your code in C++11 (g++ 4.8.2) unordered_map<int, int> um; for (size_t i = 0; i < A.size(); i++) { if (um.find(A[i]) != um.end()) { um[A[i]]++; } else { um[A[i]] = 1; } if (um[A[i]] > A.size() / 2) return i; } return -1;}