Skip to content

Instantly share code, notes, and snippets.

@wperron
Created June 27, 2023 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wperron/b9368ba0fcc65586553762f366eb8461 to your computer and use it in GitHub Desktop.
Save wperron/b9368ba0fcc65586553762f366eb8461 to your computer and use it in GitHub Desktop.
Find missing elements in sorted set of 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