Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- vector<int> a(n);
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- int res = -1;
- int i = 0;
- int j = 0;
- // create two pointersL i and j
- // i = start pointer of the element
- // j = moving pointer of the element
- // once j reaches the last occurrence of the element,
- // get the difference between j and i (frequency)
- for (i = 0; i < (int) a.size(); i = j) {
- while (j < (int) a.size() && a[i] == a[j]) {
- j++;
- }
- if ((j - i) & 1) {
- res = a[i];
- }
- }
- cout << res << '\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment