Find the missing letter in an array

6.8k views Asked by At

I am trying to create a function that takes an array input of consecutive characters in the alphabet and returns the missing letter if there is one (there will only be 1 missing letter and each element in the array will be listed in alphabetical order).

Example inputs:

['a', 'b', 'c', 'e'] -> 'd'
['l', 'n', 'o', 'p'] -> 'm'
['s', 't', 'u', 'w', 'x'] -> 'v'

const findMissingLetter = () => {
const stepOne = (array) => {
    for (let i = 0; i < array.length; i++) {
        let x = array.charCodeAt(i + 1);
        let y = array.charCodeAt(i);
            if ((x - y) != 1) {
                return (array.charCodeAt[i] + 1);
            }
  }
}
}
return findMissingLetter(stepOne.fromCharCode(array));

What I am attempting to do is loop through each index of the array and convert each character to unicode. If the [i + 1] - [i] elements in the array equal 1, then there is no missing letters. However if it does not equal 1, then I want to return the unicode of [i] + 1 and then through the higher order function convert the unicode output back to the corresponding character in the alphabet.

Can someone please explain what I am doing wrong? I know I am not calling the functions correctly.

Thanks!

4

There are 4 answers

0
Samuli Hakoniemi On

I came with this solution:

const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

const findMissing = (arr) => {
  const start = alphabet.indexOf(arr[0]);
  const end = alphabet.indexOf(arr[arr.length-1]);
  const sliced = alphabet.slice(start, end + 1);
  
  return sliced.filter((letter) => !arr.includes(letter));
};

console.log(findMissing(['a','b','c','e','h']));
console.log(findMissing(['h','j','l','p']));
console.log(findMissing(['s','t','v','z']));

This will return an array of missing letter(s) between the starting and ending alphabet letter.

0
Ori Drori On

The string method .charCodeAt() doesn't work on arrays. You need to use it on each character, and get the code at position 0 (the default):

const findMissingLetter = (array) => {
  // we can skip the 1st letter
  for (let i = 1; i < array.length; i++) {
    // get the char code of the previous letter
    const prev = array[i - 1].charCodeAt();
    // get the char code of the current letter
    const current = array[i].charCodeAt();
    
    if (current - prev !== 1) { // if the difference between current and previous is not 1
      // get the character after the previous 
      return String.fromCharCode(prev + 1);
    }
  }
  
  return null; // if nothing is found
}

console.log(findMissingLetter(['a', 'b', 'c', 'e'])); // d
console.log(findMissingLetter(['l', 'n', 'o', 'p'])); // m
console.log(findMissingLetter(['s', 't', 'u', 'w', 'x'])); // v
console.log(findMissingLetter(['a', 'b', 'c'])); // null

And another solution that uses Array.findIndex() to find the missing character:

const findMissingLetter = (array) => {
  const index = array
    .slice(1) // create an array with 1st letter removed
    .findIndex((c, i) => c.charCodeAt() - array[i].charCodeAt() > 1); // compare current letter and the same index in the original array 'till you find a missing letter
    
  return index > -1 ? // if we found an index
    String.fromCharCode(array[index].charCodeAt() + 1) // if index was found return next letter after the last letter in a sequence
    : 
    null; // if index wasn't found
};

console.log(findMissingLetter(['a', 'b', 'c', 'e'])); // d
console.log(findMissingLetter(['l', 'n', 'o', 'p'])); // m
console.log(findMissingLetter(['s', 't', 'u', 'w', 'x'])); // v
console.log(findMissingLetter(['a', 'b', 'c'])); // null

0
Ele On

Using English letters, you can use an array with the letters and check for each letter in the passed array.

let findMissing = (arr) => {
  let alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

  let j = alpha.indexOf(arr[0]);
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].toUpperCase() !== alpha[j].toUpperCase()) return alpha[j];
    
    j++;
  }

  return "";
}

console.log(findMissing(['a', 'b', 'c', 'e']));
console.log(findMissing(['l', 'n', 'o', 'p']));
console.log(findMissing(['s', 't', 'u', 'w', 'x']));
console.log(findMissing(['s', 't', 'u', 'v', 'w']));

0
trincot On

You can use charCodeAt, but on the (single-character) strings in the array:

const findMissingLetter = arr => (cur =>
    String.fromCharCode(arr.find(ch => ch.charCodeAt() != cur++)?.charCodeAt() - 1)
)(arr[0].charCodeAt());

console.log(findMissingLetter(['a', 'b', 'c', 'e'])); // 'd'
console.log(findMissingLetter(['l', 'n', 'o', 'p'])); // 'm'
console.log(findMissingLetter(['s', 't', 'u', 'w', 'x'])); // 'v'
console.log(findMissingLetter(['a', 'b', 'c'])); // ''