4 lines simple solution JAVA by bit manipulation with O(n) time


  • 0
    W

    public class Solution {

    public int  singleNumber(int[] nums) {
          int res=nums[0];
          for(int i=1;i<nums.length;i++)
              res=(int)((res|~nums[i]) & (~res|nums[i]));
                   //(a or !b) and (!a or b) 1 for same; 0 for  different
          return res;   
               }
       }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.