给定一个数组,除了一个数出现1次之外,其余数都出现3次。找出出现一次的数。
如:{1, 2, 1, 2, 1, 2, 7}, 找出7.
格式:
第一行输入一个数n,代表数组的长度,接下来一行输入数组A[n],(输入的数组必须满足问题描述的要求),最后输出只出现一次的数。
要求:
你的算法只能是线性时间的复杂度,并且不能使用额外的空间哦~
输入:
4
0 0 0 5输出: 5
C语言代码:
#include <stdio.h>
int main()
{
int count=0;
int N=0;
int array[10000]={0};
scanf("%d",&N);
for(int i=0;i<N;i++)
scanf("%d",&array[i]);
for(int x=0;x<N;x++)
{
for(int y=0;y<N;y++)
if(array[x]==array[y])
count++;
if(count==1)
printf("%d\n",array[x]);
count=0;
}
return 0;
}