JavaScript Program to Find Missing Characters to Make a String Pangram
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 w3wiki
Output : abdhijnpquvxyz
Input : The quick brown fox jumps
Output : adglvyz
Table of Content
- Using Set
- Using includes() Method
- Using indexOf() Method
- Using a Map
- Using Set and Range of ASCII Values
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.
let inputStr = "welcome to w3wiki";
let letters = "abcdefghijklmnopqrstuvwxyz";
let inputSet = new Set(inputStr);
let missingChars = [...letters]
.filter(char => !inputSet.has(char)).join('');
console.log(missingChars);
Output
abdhijnpquvxyz
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.
let inputStr = "welcome to w3wiki";
let letters = "abcdefghijklmnopqrstuvwxyz";
let output = "";
inputStr = inputStr.toLowerCase();
for (let c of letters) {
if (!inputStr.includes(c)) {
output += c;
}
}
console.log(output);
Output
abdhijnpquvxyz
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.
let inputStr = "welcome to w3wiki";
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
Using a Map
Using a Map to find missing characters for a pangram involves initializing a map for all alphabet letters, updating it with characters from the string, and then collecting keys from the map with zero occurrences.
Example: In this example we finds missing characters to make a string a pangram using a Map. It initializes the map with alphabet letters, counts occurrences, and identifies missing characters.
function findMissingPangramChars(str) {
const freqMap = new Map();
for (let i = 0; i < 26; i++) {
freqMap.set(String.fromCharCode(97 + i), 0);
}
str.toLowerCase().split('').forEach(char => {
if (freqMap.has(char)) {
freqMap.set(char, freqMap.get(char) + 1);
}
});
const missingChars = [];
for (let [char, count] of freqMap) {
if (count === 0) {
missingChars.push(char);
}
}
return missingChars.join('');
}
console.log(findMissingPangramChars("The quick brown fox jumps over the lazy dog"));
console.log(findMissingPangramChars("Hello, World!"));
Output
abcfgijkmnpqstuvxyz
Using Set and Range of ASCII Values
In this approach, we utilize a set to keep track of characters present in the input string. We iterate through the input string and add each character to the set. Then, we iterate through a range of ASCII values representing lowercase alphabets (97 to 122), checking if each character exists in the set. If not found, it is considered missing and added to the output string.
Example:
function findMissingChars(inputStr) {
let missingChars = '';
const charSet = new Set(inputStr.toLowerCase());
// Iterate through ASCII range of lowercase alphabets
for (let ascii = 97; ascii <= 122; ascii++) {
const char = String.fromCharCode(ascii);
if (!charSet.has(char)) {
missingChars += char;
}
}
return missingChars;
}
const inputStr = "welcome to w3wiki";
console.log(findMissingChars(inputStr)); // Output: abdhijnpquvxyz
Output
abdhijnpquvxyz
Contact Us