My Recursive Solution


  • 0
    V
       public class Solution {
         public int singleNonDuplicate(int[] nums) {
               return (nums.length < 3) ? -1 : (nums[0] != nums[1] ? nums[0] : single(nums,0));
        }
        public int single(int[] nums, int index){
           return (index+2 >= nums.length) ? nums[index+1] : (nums[index] != nums[index+1] && nums[index+1] != nums[index+2] ? nums[index+1] : single(nums,index+1));
         }
    }

Log in to reply
 

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