Concise JavaScript solution that works locally is giving wrong answer on LeetCode


  • 0
    F

    Here is my solution:

    /**
     * @param {number[]} nums
     * @return {number[]}
     */
    var removeDuplicates = function (nums) {
      var firstIndex = 0, result = [];
      nums.forEach(function (curr, index, arr) {
        if (arr[index] === arr[index - 1]) {
          if (index === firstIndex + 1) {
            result.push(curr);
          }
        } else {
          firstIndex = index;
          result.push(curr);
        }
      });
      return result;
    };
    

    I am getting Wrong Answer for the input of [1, 1] which seems to be returning [] on LeetCode, and is giving me the input itself ([1, 1]) locally.

    Can anyone help me understand the problem?


  • 0
    R

    same problem


  • 2
    Q

    hey, i got same problem and i solved it.
    please note
    'Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.'
    that means the result should be stored in nums other than in a new array.beside ,you should return the number of duplicates elements.


Log in to reply
 

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