findErrorNums.js

    0

    0

    This code extracts the second lowest number from a list of numbers. It checks if the number is negative by subtracting 1 from it and comparing the result to 0. If the number is less than zero, the number is added to a list and the loop iterates again. If the number is greater than zero, the number is added to the list and the loop iterates again. The code then returns the list of numbers with the second lowest number in it.

    
    var findErrorNums = function(nums) {
      const res = [];
      
      for (let num of nums) {
          num = Math.abs(num);
          
          if (nums[num - 1] < 0) res.push(num);
          else nums[num - 1] = -nums[num - 1];
      }
    
      for (let i = 0; i < nums.length; i++) {
          if (nums[i] > 0) res.push(i + 1);
          if (res.length === 2) return res;
      }
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.