Skip to content

只出现一次的数字


题解


思路

与或运算

js
/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function (nums) {
  let ret = 0;

  for (const item of nums) {
    ret ^= item;
  }

  return ret;
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function (nums) {
  let ret = 0;

  for (const item of nums) {
    ret ^= item;
  }

  return ret;
};