Given an array of integers, every element appearstwiceexcept for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Subscribeto see which companies asked this question.

Hide Tags

Hash Table

Bit Manipulation

Hide Similar Problems

(M) Single Number II

(M) Single Number III

(E) Missing Number

(M) Find the Duplicate Number

(E) Find the Difference


public class Solution {
    public int singleNumber(int[] nums) {
          /**
         * 异或运算符^:两个操作数的位中,相同则结果为0,不同则结果为1
         * 
         * 所以
         * n^n = 0
         * n^0 = n
         */
         int result = 0;
         for(int num : nums){
             result ^= num;
         }
         return result;
    }
}

results matching ""

    No results matching ""