美文网首页
Find the odd int

Find the odd int

作者: Magicach | 来源:发表于2017-12-25 22:35 被阅读0次

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);
  }
}

相关文章

网友评论

      本文标题:Find the odd int

      本文链接:https://www.haomeiwen.com/subject/wivrgxtx.html