JavaScript Program to Get a Non-Repeating Character From the Given String
Last Updated :
04 Mar, 2024
In JavaScript, we can find the non-repeating character from the input string by identifying the characters that occur only once in the string.
There are several approaches in JavaScript to get a non-repeating character from the given string which are as follows:
Using indexOf and lastIndexOf methods
In this approach, we are using the indexOf and lastIndexOf methods to iterate through each character of the given string (str). We check if the current character has the same index as its last occurrence; if true, it indicates the character is non-repeating, and we store it in the variable res.
Syntax:
let index = array.indexOf(element);
let lastIndex = array.lastIndexOf(element);
Example: The below example uses indexOf and lastIndexOf methods to Get a Non-Repeating Character From the Given String.
JavaScript
let str = "geeksforgeeks";
let res = null;
for (let char of str) {
if (
str.indexOf(char)
===
str.lastIndexOf(char)
) {
res = char;
break;
}
}
console.log("Input: " + str);
console.log("Non-repeating character: " + res);
OutputInput: geeksforgeeks
Non-repeating character: f
Using Set and Array.from()
In this approach, we are using a Set (s) to track unique characters and another Set (temp) to store repeating characters while iterating through each character of the given string (str). We convert the unique characters from the first set into an array using Array.from() and find the non-repeating character by filtering out those present in the second set (temp), storing it to the variable res.
Syntax:
let mySet = new Set();
let myArray = Array.from(iterable);
Example: The below example uses Set and Array.from() to Get a Non-Repeating Character From the Given String.
JavaScript
let str = "geeksforgeeks";
let s = new Set();
let temp = new Set();
for (let char of str) {
if (s.has(char)) {
temp.add(char);
}
else {
s.add(char);
}
}
let res = Array.from(s)
.find(char => !temp.has(char));
console.log("Input: " + str);
console.log("Non-repeating character: " + res);
OutputInput: geeksforgeeks
Non-repeating character: f
Using Map
In this approach, we are using Map (m) to track the indices of characters in the given string (str). During iteration, if a character is encountered again, its index is marked as -1, and the non-repeating character is determined by finding the one with the smallest non-negative index in the map. The result is stored in the variable res.
Syntax:
let res = new Map();
Example: The below example uses Map to Get a Non-Repeating Character From the Given String.
JavaScript
let str = "geeksforgeeks";
let m = new Map();
let res = null;
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (m.has(char)) {
m.set(char, -1);
} else {
m.set(char, i);
}
}
m.forEach((index, char) => {
if (
index !== -1
&&
(res === null || index < m.get(res))
) {
res = char;
}
});
console.log("Input: " + str);
console.log("Non-repeating character: " + res);
OutputInput: geeksforgeeks
Non-repeating character: f
Similar Reads
JavaScript Program to Find Kâth Non-Repeating Character in String The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps
6 min read
JavaScript Program to Check for Repeated Characters in a String Here are the different methods to check for repeated characters in a string using JavaScript1. Using a Frequency Counter (Object)A frequency counter is one of the most efficient ways to check for repeated characters in a string. This approach involves iterating over the string and counting how often
3 min read
JavaScript Program to Remove Consecutive Duplicate Characters From a String We are going to implement a JavaScript program to remove consecutive duplicate characters from a string. In this program, we will eliminate all the consecutive occurrences of the same character from a string.Example:Input: string: "geeks" Output: "geks"Explanation :consecutive "e" should be removedT
5 min read
JavaScript Program to Print All Duplicate Characters in a String In this article, we will learn how to print all duplicate characters in a string in JavaScript. Given a string S, the task is to print all the duplicate characters with their occurrences in the given string. Example: Input: S = âgeeksforgeeksâOutput:e, count = 4g, count = 2k, count = 2s, count = 2Ta
5 min read
Javascript Program To Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below
5 min read
Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided
2 min read