If there's a number written odd number of times, it's impossible to pair that number entirely, inevitably leaving one left.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> cnt(10);
for (int i = 0; i < 5; i++)
{
int Ai;
cin >> Ai;
cnt[Ai]++;
}
for (int i = 0; i < 10; i++)
if (cnt[i] % 2 == 1)
cout << i << endl;
}
def main():
cnt = [0] * 10
for i in range(5):
Ai = int(input())
cnt[Ai] += 1
for i in range(10):
if cnt[i] % 2 == 1:
print(i)
if __name__ == '__main__':
main()
The properties of the XOR (exclusive OR) operation are as follows:
Due to the properties of XOR, if you XOR the same number twice, the number will disappear. Thus, to find the only number that can't be paired, XOR all the numbers given in the input.
#include <iostream>
using namespace std;
int main()
{
int ans = 0;
for (int i = 0; i < 5; i++)
{
int Ai;
cin >> Ai;
ans ^= Ai;
}
cout << ans << endl;
}
def main():
ans = 0
for i in range(5):
Ai = int(input())
ans ^= Ai
print(ans)
if __name__ == '__main__':
main()