Skip to content

Instantly share code, notes, and snippets.

@jlcarrascof
Created August 25, 2023 15:57
Show Gist options
  • Save jlcarrascof/c1e8091d49de0af7ef87e944a0d3049a to your computer and use it in GitHub Desktop.
Save jlcarrascof/c1e8091d49de0af7ef87e944a0d3049a to your computer and use it in GitHub Desktop.
Challenge resolved in our third activity at Microverse - Week 3 - Day 5
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