without bit manipulation


  • 0
    C
    public class Solution {
        public int singleNumber(int[] nums) {
            if(nums.length==1){
                return nums[0];
            }
            Arrays.sort(nums);
            if(nums[0]!=nums[1]){
                return nums[0];
            }
            for(int i = 1;i<nums.length-1;i++){
                if(nums[i]!=nums[i-1] && nums[i]!=nums[i+1]){
                    return nums[i];
                }
            }
            return nums[nums.length-1];
        }
    }
    

Log in to reply
 

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