[JavaScript] I'm using [].filter(), but why it can't work?


  • 0
    A
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var removeDuplicates = function(nums) {
        var temp;
        nums = nums.filter(function(item) {
            if (temp !== null && temp !== item) {
                temp = item;
                return true;
            } else {
                return false;   
            }
        });
        return nums.length;
    };
    

    0_1494564315339_Screen Shot 2017-05-11 at 9.44.49 PM.png

    Thanks!!


  • 0
    A

    Also, I have tried to log those results, the results in log data look good. For example, in the test in the screenshot console.log(nums) is [1, 2] actually.


Log in to reply
 

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