Open In App

JavaScript Program to Get a Non-Repeating Character From the Given String

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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);


Output

Input: 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);


Output

Input: 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);


Output

Input: geeksforgeeks
Non-repeating character: f


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads