Created
June 27, 2023 20:17
-
-
Save wperron/b9368ba0fcc65586553762f366eb8461 to your computer and use it in GitHub Desktop.
Find missing elements in sorted set of characters
This file contains 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
/** | |
* Write a function that takes an array of consecutive, increasing letters as | |
* input, and returns any missing letters in the array between the first and | |
* last letter. | |
* | |
* Example: | |
* ``` | |
* > missingLetters(['a','b','c','d','f']) | |
* > ['e'] | |
* | |
* > missingLetters(['a','b','c','d','e','h','i','j','k','l','m','n','o','p','q','r','s','t','u','w','x','y','z']) | |
* > ['f','g','v'] | |
* ``` | |
*/ | |
// constant of all letters | |
const LETTERS = 'abcdefghijklmnopqrstuvwxyz'.split(''); | |
/** | |
* returns an array with the letters missing from the array `a` | |
* @param {Array<string>} a an array of letters, assumed to be sorted | |
*/ | |
export function findMissing(a) { | |
let chunk = new Set(LETTERS.slice( | |
LETTERS.indexOf(a[0]), | |
LETTERS.indexOf(a[a.length-1])+1 | |
)); | |
for (const c of a) { | |
chunk.delete(c) | |
} | |
return Array.from(chunk) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment