How to find unique characters of a string in JavaScript ?
Last Updated :
03 Jan, 2024
This article aims to find unique characters in a string using JavaScript. This means that a character occurs once in the string then we cannot include it again.
Example:
Input: Geeksforgeeks
Output: Geksforg
Input: Geeksforgeeks is a great site for computer science
Output: Geksforg Iaticmpun
We have a few methods to find unique characters in a string using JavaScript which are described below:
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 iterate over the string using the for a loop at every iteration, we check if the uniq contains the character.
Example: This example shows the above-explained approach.
Javascript
function findUnique(str) {
let uniq = "" ;
for (let i = 0; i < str.length; i++) {
if (uniq.includes(str[i]) === false ) {
uniq += str[i]
}
}
return uniq;
}
console.log(findUnique( "Geeksforgeeks" ))
console.log(findUnique( "Geeksforgeeks Is great" ))
|
Output
Geksforg
Geksforg Iat
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
function findUnique(str) {
str = str.split( "" )
str = new Set(str);
str = [...str].join( "" );
return str;
}
console.log(findUnique( "Geeksforgeeks" ))
console.log(findUnique( "Geeksforgeeks Is a great site" ))
|
Output
Geksforg
Geksforg Iati
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
function findUnique(str) {
return [...str].reduce((acc, curr) => {
return acc.includes(curr) ? acc : acc + curr;
}, "" )
}
console.log(findUnique( "Geeksforgeeks" ))
console.log(findUnique( "Geeksforgeeks Is great" ))
|
Output
Geksforg
Geksforg Iat
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( "Geeksforgeeks" ));
|
Approach 5: Using regex
In this approach, we will use the regex expression for finding the unique element in the string.
Example: In this example, we are using regex.
Javascript
function uniqueChar(str) {
let uniqueChars = str.match(/(.)(?!\1)/g);
return uniqueChars;
}
let str = "Geeksforgeeks" ;
let newStr = uniqueChar(str);
console.log(newStr);
|
Output
[
'G', 'e', 'k', 's',
'f', 'o', 'r', 'g',
'e', 'k', 's'
]
The _.uniq method is used to create an array of unique values in order, from all given arrays using SameValueZero for equality comparisons.
Example: In this example, we are using the Lodash _.uniq() method to find the unique character.
Javascript
const _ = require( "lodash" );
let y = "Geeksforgeeks" ;
let gfg = _.uniq(y).join( '' );
console.log(gfg);
|
Output:
Geksforg
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...