Easy only if statement 2ms


  • 0
    R
    public class Solution {
        public int singleNonDuplicate(int[] nums) {
            int val=0;
            for(int i=0;i<nums.length;i++)
            {
                if(i!=0&&i!=nums.length-1){
                    if(nums[i]!=nums[i-1]&&nums[i]!=nums[i+1])  return nums[i];
                }
                if(i==0){
                    if(nums[i]!=nums[i+1]) return nums[i];
                }
                if(i==nums.length-1){
                    if(nums[i]!=nums[i-1]) return nums[i];
                }
            }
            return val;
        }
    }
    

    returns immediatly when result found.


Log in to reply
 

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