payal-kothari
7/18/2017 - 9:21 PM

From https://leetcode.com/problems/single-number/#/description

public class Solution {
    public int singleNumber(int[] nums) {
        
        Arrays.sort(nums);          //***
        
        int i =0;
        int j = 0;
        while (i< nums.length){
            if ( i < nums.length -1){
                j = i+1;
            }
            
            if (nums[i] != nums[j]){
                return nums[i];
            }else{
                i = i+2;
            }
        }
        return nums[0];
        
    }
}
public class Solution {
    public int singleNumber(int[] nums) {
 //************ Better solution  using XOR 
        // 0 ^ N = N
        // N ^ N = 0
        
        int ans = 0;
        
        for(int n : nums){
            ans = ans ^ n;
        }
        
        return ans;
    }
}