codewar卡塔
Given an array, find the int that appears an odd number of times.
There will always be only one integer that appears an odd number of times.
给定一个数组,找到出现奇数次的int。
始终只有一个整数出现奇数次。
public class FindOdd {
public static int findIt(int[] a) {
int odd = 0;
for (int i = 0;i<a.length;i++){
odd ^= a[i];//0与任何数异或都为该数本身,两个一样的数异或为0
}
return odd;
}
}
网友评论