Find the number that appears only once (find out single dogs)

Find the number that has only appeared once (find out single dogs)

Tip: Use the bitwise XOR operator to quickly find a single number. The XOR of two identical numbers is zero, and the XOR of zero and any number is any number.

#include<stdio.h>
int main()
{
    
    
	//其他数都是成对出现,只有一个数出现了一次,请找出他
	int array[5] = {
    
     1, 1, 3,3,7 };
	int i = 0;
	int ret = 0;
	while (i < 5)
	{
    
    
		ret = ret^array[i];//1^1=0,3^3=0,0^7=7
		i = i + 1;
	}
	printf("单身狗是%d\n", ret);

	

}

Guess you like

Origin blog.csdn.net/weixin_45796387/article/details/110096884