pickingNumbers.js

    0

    0

    1, 2, 2, 3, 1, 2

    function pickingNumbers(a) {
      // Write your code here
      let sortedArray = a.sort(function (a, b) {
        return a - b;
      });
    
      let currentArray = [];
      let longestArray = 0;
      let startNumber = 0;
      for (let i = 0; i < sortedArray.length; i++) {
        let result = Math.abs(sortedArray[startNumber] - sortedArray[i]);
        if (result <= 1) {
          currentArray.push(sortedArray[i]);
          if (currentArray.length > longestArray) {
            longestArray = currentArray.length;
          }
        } else {
          startNumber = i;
          if (currentArray.length > longestArray) {
            longestArray = currentArray.length;
          }
          currentArray = [];
          currentArray.push(sortedArray[i]);
        }
      }
      return longestArray;
    }
    
    pickingNumbers([1, 2, 2, 3, 1, 2]);
    
    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.