一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。
#include<unordered_map>
class Solution {
public:
//num1和num2分别保存这两个只出现一次的数字
void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
int len=data.size();
unordered_map<int,int> hashMap;
for(int i=0;i<len;i++)
{
if(hashMap.find(data[i])==hashMap.end())
{
hashMap[data[i]]=1;
}
else
{
hashMap[data[i]]++;
}
}
unordered_map<int,int>::iterator it;
vector<int> res;
for(it=hashMap.begin();it!=hashMap.end();it++)
{
if(it->second==1)
{
res.push_back(it->first);
}
}
*num1=res[0];
*num2=res[1];
}
};