Open In App

JavaScript Program to Find Missing Characters to Make a String Pangram

Last Updated : 03 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We have given an input string and we need to find all the characters that are missing from the input string. We have to print all the output in the alphabetic order using JavaScript language. Below we have added the examples for better understanding.

Examples:

Input : welcome to geeksforgeeks
Output : abdhijnpquvxyz
Input : The quick brown fox jumps
Output : adglvyz

Approach 1: Using Set

In this approach, we use the Set in JavaScript to create the characters from the input string and remove the duplicate, and then go through the alphabet characters. Each alphabet letter is been checked if it is present in the Set. If it is not present then we add it to the output variable and printing it.

Example: In this example, we will print Missing characters to make a string Pangram in JavaScript using Set.

Javascript




let inputStr = "welcome to geeksforgeeks";
let letters = "abcdefghijklmnopqrstuvwxyz";
let inputSet = new Set(inputStr);
let missingChars = [...letters]
    .filter(char => !inputSet.has(char)).join('');
console.log(missingChars);


Output

abdhijnpquvxyz

Approach 2: Using includes() Method

In this approach, we are using the includes() method. Here we have used the for loop to go through the each character of the letter variable. For every letter, we are checking if the character is present in the inputStr variable using the includes() method. If the character is not present, then it is added in the output variable and printed.

Example: In this example, we will print Missing characters to make a string Pangram in JavaScript using includes() Method.

Javascript




let inputStr = "welcome to geeksforgeeks";
let letters = "abcdefghijklmnopqrstuvwxyz";
let output = "";
inputStr = inputStr.toLowerCase();
for (let c of letters) {
    if (!inputStr.includes(c)) {
        output += c;
    }
}
console.log(output);


Output

abdhijnpquvxyz

Approach 3: Using indexOf() Method

In this apporach, we are using the indexOf() method. Here, we have used the for loop to go through the chacter-by-character of the letters string. Every character, we check if the letters variable cgacater exists in the input string using indexOf() method. If the chactaer is found then it is been ignored, if it is not found then the character is added in the output string.

Example: In this example, we will print Missing characters to make a string Pangram in JavaScript using indexOf() method.

Javascript




let inputStr = "welcome to geeksforgeeks";
let letters = "abcdefghijklmnopqrstuvwxyz";
let output = "";
inputStr = inputStr.toLowerCase();
for (let i = 0; i < letters.length; i++) {
    if (inputStr.indexOf(letters[i]) === -1) {
        output += letters[i];
    }
}
console.log(output);


Output

abdhijnpquvxyz


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads