Find the odd int

2017-12-25  本文已影响0人  Magicach

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.

Good Solution 1:

public class FindOdd {
  public static int findIt(int[] A) {
    int xor = 0;
    for (int i = 0; i < A.length; i++) {
      xor ^= A[i];
    }
    return xor;
  }
}

Good Solution 2:

import static java.util.Arrays.stream;

public class FindOdd {
  public static int findIt(int[] arr) {
    return stream(arr).reduce(0, (x, y) -> x ^ y);
  }
}
上一篇 下一篇

猜你喜欢

热点阅读