JavaScript Program to Find Missing Characters to Make a String Pangram
Last Updated :
31 Jul, 2024
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
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);
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);
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);
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.
JavaScript
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!"));
Outputabcfgijkmnpqstuvxyz
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:
JavaScript
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 geeksforgeeks";
console.log(findMissingChars(inputStr)); // Output: abdhijnpquvxyz
Using Regular Expressions
In this approach, we use regular expressions to identify which letters of the alphabet are missing from the input string. We convert the input string to lowercase and create a regular expression that matches any character not found in the string. Finally, we use this regular expression to find and collect the missing characters.
Example:
JavaScript
function findMissingCharsUsingRegex(inputStr) {
const letters = "abcdefghijklmnopqrstuvwxyz";
const lowerInputStr = inputStr.toLowerCase();
let missingChars = '';
// Iterate through each letter in the alphabet
for (let i = 0; i < letters.length; i++) {
const char = letters[i];
const regex = new RegExp(char, 'g');
if (!regex.test(lowerInputStr)) {
missingChars += char;
}
}
return missingChars;
}
let inputStr = "welcome to geeksforgeeks";
console.log(findMissingCharsUsingRegex(inputStr)); // Output: abdhijnpquvxyz
inputStr = "The quick brown fox jumps";
console.log(findMissingCharsUsingRegex(inputStr)); // Output: adglvyz
// Contributed by Nikunj Sonigara
Outputabdhijnpquvxyz
adglvyz
Approach: Using Bitwise Operations
In this approach, we utilize bitwise operations to determine which characters are missing from the input string. Each bit in an integer represents a different letter of the alphabet. By setting bits corresponding to the letters found in the input string, we can efficiently check which letters are missing.
Example:
This example demonstrates the use of bitwise operations to find missing characters in the input string.
JavaScript
function findMissingCharacters(inputStr) {
// Initialize checker to zero
let checker = 0;
// Iterate through each character in the input string
for (let char of inputStr.toLowerCase()) {
if (char >= 'a' && char <= 'z') {
// Calculate the bit position for the character
let bitPosition = char.charCodeAt(0) - 'a'.charCodeAt(0);
// Set the bit at the corresponding position
checker |= (1 << bitPosition);
}
}
// Initialize the result string
let result = '';
// Check for missing characters
for (let i = 0; i < 26; i++) {
// If the bit at position i is not set, add the character to the result
if ((checker & (1 << i)) === 0) {
result += String.fromCharCode('a'.charCodeAt(0) + i);
}
}
return result;
}
// Test cases
console.log(findMissingCharacters("welcome to geeksforgeeks")); // Output: abdhijnpquvxyz
console.log(findMissingCharacters("The quick brown fox jumps")); // Output: ad
Outputabdhijnpquvxyz
adglvyz
Using reduce() Method
In this approach, we use the reduce() method to accumulate the missing characters. We iterate through each character in the alphabet and check if it is present in the input string using the includes() method. If the character is not present, it is added to the accumulator, which holds the missing characters.
Example:
JavaScript
function findMissingCharsUsingReduce(inputStr) {
let letters = "abcdefghijklmnopqrstuvwxyz";
let lowerInputStr = inputStr.toLowerCase();
let missingChars = letters.split('').reduce((acc, char) => {
if (!lowerInputStr.includes(char)) {
acc += char;
}
return acc;
}, '');
return missingChars;
}
let inputStr = "welcome to geeksforgeeks";
console.log(findMissingCharsUsingReduce(inputStr));
inputStr = "The quick brown fox jumps";
console.log(findMissingCharsUsingReduce(inputStr));
Outputabdhijnpquvxyz
adglvyz
Using Array Difference
In this approach, we first create an array of all lowercase letters. Then, we filter out the letters that are present in the input string to get the missing characters.
Example:
JavaScript
function findMissingCharsUsingArrayDifference(inputStr) {
const letters = "abcdefghijklmnopqrstuvwxyz".split('');
const inputChars = inputStr.toLowerCase().split('');
const missingChars = letters.filter(char => !inputChars.includes(char)).join('');
return missingChars;
}
let inputStr = "welcome to geeksforgeeks";
console.log(findMissingCharsUsingArrayDifference(inputStr));
inputStr = "The quick brown fox jumps";
console.log(findMissingCharsUsingArrayDifference(inputStr));
Outputabdhijnpquvxyz
adglvyz
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read