题目:只允许遍历一次,找出只出现了一次的数
题目分析:只允许遍历一次,所以这里用到位操作,当两个相同的数字异或时,返回值为0,所以经过一系列的异或后,返回值为只出现了一次的那个数。
代码如下:
public class TestDome1 { public static void main(String[] args) { int[] array = new int [10]; int[] array2 = {1,3,1,5,5}; int temp = 0; for (int i = 0; i < 5; i++) { temp ^= array2[i]; } System.out.println(temp); } }