给定一个数组,除了一个数出现1次之外,其余数都出现3次。找出出现一次的数。
如:{1, 2, 1, 2, 1, 2, 7}, 找出7.
格式:
第一行输入一个数n,代表数组的长度,接下来一行输入数组A[n],(输入的数组必须满足问题描述的要求),最后输出只出现一次的数。
要求:
你的算法只能是线性时间的复杂度,并且不能使用额外的空间哦~
样例输入
4
0 0 0 5
样例输出
5
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
map<int,int> m;
map<int,int>:: iterator it;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
m[k]++;
}
for(it=m.begin();it!=m.end();it++)
if(it->second==1)
cout<<it->first;
}
return 0;
}