Not the shortest but easiest to understand


  • 0
    S

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


Log in to reply
 

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