Home » Javascript » Recursive function returns undefined even with a valid return statement in javascript

Recursive function returns undefined even with a valid return statement in javascript

Posted by: admin November 1, 2017 Leave a comment

Questions:

This is a problem where I need to return a number which only appears once in the array while the rest of the elements appear twice. Using binary search I did what I can but I was stuck at the first test case. I don’t understand why it’s returning undefined when I can log the same. Help me understand what’s going on.

function singleNonDuplicate(nums) {
  if(nums.length == 1) {
     console.log(nums[0]);
     return nums[0];
  } 

  if((nums[Math.floor(nums.length/2)] !== nums[Math.floor(nums.length/2) - 1]) && (nums[Math.floor(nums.length/2)] !==                                    nums[Math.floor(nums.length/2) + 1])) {
      return nums[Math.floor(nums.length/2)];
  } 


  if(Math.floor(nums.length/2) % 2  == 0) {
      if(nums[Math.floor(nums.length/2)] == nums[Math.floor(nums.length/2) - 1]) {
          singleNonDuplicate(nums.slice(0, Math.floor(nums.length/2) - 1));
                      console.log('g');
      } else {
          singleNonDuplicate(nums.slice(Math.floor(nums.length/2) + 2));
                      console.log('g');

      }
  } else {
       if(nums[Math.floor(nums.length/2)] == nums[Math.floor(nums.length/2) - 1]) {
           console.log(nums.slice(Math.floor(nums.length/2) + 1));
          singleNonDuplicate(nums.slice(Math.floor(nums.length/2) + 1));
      } else {
          console.log('g');
          singleNonDuplicate(nums.slice(0, Math.floor(nums.length/2)));
      }
  }
}

console.log(singleNonDuplicate([1,1,2]));
Answers:

This might be a little off topic but there is a much better way to do this kind of problem using XOR:

function appearsOnlyOnce(array) {
    var single = array[0];
    for (var i=1; i<array.length; i++) {
        single ^= array[i];
    }
    return single
}

Questions:
Answers:

When you call singleNonDuplicate from within your functio you have to return the result.

function singleNonDuplicate(nums) {
  if(nums.length == 1) {
     console.log(nums[0]);
     return nums[0];
  } 

  if((nums[Math.floor(nums.length/2)] !== nums[Math.floor(nums.length/2) - 1]) && (nums[Math.floor(nums.length/2)] !==                                    nums[Math.floor(nums.length/2) + 1])) {
      return nums[Math.floor(nums.length/2)];
  } 


  if(Math.floor(nums.length/2) % 2  == 0) {
      if(nums[Math.floor(nums.length/2)] == nums[Math.floor(nums.length/2) - 1]) {
          return singleNonDuplicate(nums.slice(0, Math.floor(nums.length/2) - 1));
                      console.log('g');
      } else {
          return singleNonDuplicate(nums.slice(Math.floor(nums.length/2) + 2));
                      console.log('g');

      }
  } else {
       if(nums[Math.floor(nums.length/2)] == nums[Math.floor(nums.length/2) - 1]) {
           console.log(nums.slice(Math.floor(nums.length/2) + 1));
          return singleNonDuplicate(nums.slice(Math.floor(nums.length/2) + 1));
      } else {
          console.log('g');
          return singleNonDuplicate(nums.slice(0, Math.floor(nums.length/2)));
      }
  }
}

console.log(singleNonDuplicate([1,1,2]));