How to find unique characters of a string in JavaScript ?

This article aims to find unique characters in a string using JavaScript. This means that character occurs once in the string then we cannot include it again.
We have a few methods to find unique characters in a string using JavaScript which are described below:
- Using Naive Approach
- Using the Set
- Using the spread operator
- Using the indexOf() Method
- Using regex
- Using _.uniq() Method
Example:
Input: Geeksforzambiatek
Output: Geksforg
Input: Geeksforzambiatek is a great site for computer science
Output: Geksforg Iaticmpun
Approach 1: Using Naive Approach
This is a naive approach to finding unique characters from a string. In this approach, we create a variable name uniq and we are iterate over the string using the for loop at every iteration, we are checking if the uniq contains the character.
Example: This example shows the above-explained approach.
Javascript
function findUnique(str) {    // The variable that contains the unique values    let uniq = "";Â
    for (let i = 0; i < str.length; i++) {        // Checking if the uniq contains the character        if (uniq.includes(str[i]) === false) {            // If the character not present in uniq            // Concatenate the character with uniq            uniq += str[i]        }    }    return uniq;}Â
console.log(findUnique("Geeksforzambiatek"))console.log(findUnique("Geeksforzambiatek Is a great     site for computer science")) |
Geksforg Geksforg Iaticmpun
Approach 2: Using the Set
In this method, we use the set data structure. The set data structure contains only unique values, and we take advantage of it. So to extract the unique values from the string using Set we follow the steps below.
- Using the split() method convert the string into an array.
- Create a Set using new Set() and pass the converted array into it.
- Now convert the set into an array using the spread operator e.g: […set]
- And then join that array to make a string.
Example: This example shows the above-explained approach.
Javascript
// Javascript program to extract unique //characters from a stringfunction findUnique(str) {Â
    // Split the string to make array    str = str.split("")Â
    // Create a set using array    str = new Set(str);Â
    // Convert the set into array using spread     // operator and join it to make string    str = [...str].join("");Â
    return str;}Â
Â
console.log(findUnique("Geeksforzambiatek"))console.log(findUnique("Geeksforzambiatek Is a     great site for computer science")) |
Geksforg Geksforg Iaticmpun
Approach 3: Using the spread operator
In this approach first, we convert the string into an array using the spread operator e.g. […str] and then we apply the reduce method on that array.Â
Example: This example shows the above-explained approach.
Javascript
// Javascript program to extract unique characters from a stringfunction findUnique(str) {Â Â Â Â return [...str].reduce((acc, curr) => {Â Â Â Â Â Â Â Â return acc.includes(curr) ? acc : acc + curr;Â Â Â Â }, "")}Â
Â
console.log(findUnique("Geeksforzambiatek"))console.log(findUnique("Geeksforzambiatek Is     a great site for computer science")) |
Geksforg Geksforg Iaticmpun
Approach 4: Using the indexOf() Method
In this approach, we will use a for-loop in order to iterate over the complete string, and then by using the indexOf() method we will check the index of each and every character (using charAt() method) of the string which is either repeated or not repeated and then in an empty string (which we will declare initially) we will store all the unique characters.
Example: This example shows the above-explained approach.
Javascript
let uniqueCharacters = (stringg) => {Â Â Â Â let unique_characters = "";Â Â Â Â for (let i = 0; i < stringg.length; i++) {Â Â Â Â Â Â Â Â if (unique_characters.indexOf(stringg.charAt(i)) < 0) {Â Â Â Â Â Â Â Â Â Â Â Â unique_characters += stringg.charAt(i);Â Â Â Â Â Â Â Â }Â Â Â Â }Â Â Â Â return unique_characters;};console.log(uniqueCharacters("Geeksforzambiatek")); |
Geksforg
Approach 5: Using regex
In this approach, we will use the regex expression for finding the unique element in the string.
Example:
Javascript
function uniqueChar(str) {    // Use a regex pattern to match unique characters    let uniqueChars = str.match(/(.)(?!\1)/g);Â
    return uniqueChars;}let str = "Geeksforzambiatek";let newStr = uniqueChar(str);console.log(newStr); |
[ 'G', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'k', 's' ]
Approach 6: Using _.uniq() Method
The _.uniq method is used to create an array of unique values in order, from all given arrays using SameValueZero for equality comparisons.
Example:Â
Javascript
// Requiring the lodash libraryconst _ = require("lodash");Â
// Original arraylet y = "Geeksforzambiatek";Â
// Use of _.uniq()// methodÂ
let gfg = _.uniq(y).join('');Â
// Printing the outputconsole.log(gfg); |
Output:
Geksforg



