My 136ms JavaScript Solution


  • 0
    E

    Sharing my JavaScript solution since it's runtime beats 100% of other JS solutions.

    var singleNumber = function(nums) {
      var singleNums = [];
      nums.sort((a, b) => {
        return a - b;
      });
      while (nums.length > 0) {
        var num = nums.shift();
        if(nums.indexOf(num) !== -1) {
          while(nums[0] === num) {
            nums.shift();
          }
        } else {
          singleNums.push(num);
        }
      }
      return singleNums;
    };

  • 0
    M

    Array.sort if typically nlogn. This will work with bucket sort though which is linear.


Log in to reply
 

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