Created
August 25, 2023 15:57
-
-
Save jlcarrascof/c1e8091d49de0af7ef87e944a0d3049a to your computer and use it in GitHub Desktop.
Challenge resolved in our third activity at Microverse - Week 3 - Day 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pickingNumbers(a) { | |
const frequencyMap = new Map(); | |
for (let i = 0; i < a.length; i++) { | |
const num = a[i]; | |
if (frequencyMap.has(num)) { | |
frequencyMap.set(num, frequencyMap.get(num) + 1); | |
} else { | |
frequencyMap.set(num, 1); | |
} | |
} | |
let maxLength = 0; | |
const numKeys = Array.from(frequencyMap.keys()); | |
for (let i = 0; i < numKeys.length; i++) { | |
const num = numKeys[i]; | |
const frequency = frequencyMap.get(num); | |
const nextFrequency = frequencyMap.get(num + 1) || 0; | |
const currentLength = frequency + nextFrequency; | |
maxLength = Math.max(maxLength, currentLength); | |
} | |
return maxLength; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment