How to find unique characters of a string in JavaScript ?

23 Jun, 2022
The purpose of this article is to find unique characters in a string using JavaScript.
Example:
Input:  Geeksforgeeks
Output: Geksforg

Input:  Geeksforgeeks is a great site for computer science
Output: Geksforg Iaticmpun
To achieve this, we have the following approaches:
Approach 1: This is a naive approach to find the unique characters from a string. In this approach, we create a variable name uniq and we are iterating over the string using the for loop and at every iteration, we are checking if the uniq contains the character.
Javascript
<script>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("Geeksforgeeks"))console.log(findUnique("Geeksforgeeks Is a great site for computer science"))</script>
Output:
"Geksforg"

"Geksforg Iaticmpun"
Approach 2: In this method, we use the set data structure. The set data structure contains only unique values, and we take the advantage of it. So to extract the unique values from 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 spread operator e.g: [...set]
And then join that array to make a string.
Javascript
<script>// 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("Geeksforgeeks"))console.log(findUnique("Geeksforgeeks Is a great site for computer science"))</script>
Output:
"Geksforg"

"Geksforg Iaticmpun"
Approach 3: 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. 
Javascript
<script>// 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("Geeksforgeeks"))console.log(findUnique("Geeksforgeeks Is a great site for computer science"))</script>
Output:
"Geksforg"
"Geksforg Iaticmpun"
Approach-4: 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.
Javascript
// JavaScript code for the above approach... 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("Geeksforgeeks")); // This code is contributed by Aman Singla...
Output:
Geksforg
arorakashish0911
sweetyty
gulshankumarar231
simranarora5sos
amansingla
JavaScript-Methods
JavaScript-Questions
javascript-string
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.