Secret repetition
Solutions This problem is very easy but, as we’ll see in a minute, it can be solved in a very clever way.
The easiest approach uses extra space, a frequency table. The core of the algorithm in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 int N; cin >> N; vector<int> A(2*N); copy_n(istream_iterator<int>(cin), 2*N, begin(A)); vector<int> freq(1'000'000 + 1); for (auto i : A) { freq[i]++; if (freq[i] > 1) { cout << i; break; } } This solution is linear.
[Read More]