- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filtering string to contain unique characters in JavaScript
Problem
We are required to write a JavaScript function that takes in a string str. Our function should construct a new string that contains only the unique characters from the input string and remove all occurrences of duplicate characters.
Example
Following is the code −
const str = 'hey there i am using javascript'; const removeAllDuplicates = (str = '') => { let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(str.indexOf(el) === str.lastIndexOf(el)){ res += el; continue; }; }; return res; }; console.log(removeAllDuplicates(str));
Output
Following is the console output −
Ymungjvcp
- Related Articles
- Filtering array to contain palindrome elements in JavaScript
- Constructing array from string unique characters in JavaScript
- How to find unique characters of a string in JavaScript?
- Mapping unique characters of string to an array - JavaScript
- Number of non-unique characters in a string in JavaScript
- Checking if a string contains all unique characters using JavaScript
- Filtering out the non-unique value to appear only once in JavaScript
- Filtering out numerals from string in JavaScript
- How to split string values that contain special characters in R?
- Array filtering using first string letter in JavaScript
- Check that the String does not contain certain characters in Java
- Program to find length of concatenated string of unique characters in Python?
- C# program to determine if a string has all unique characters
- Python program to check if a string contains all unique characters
- Maximum Length of a Concatenated String with Unique Characters in C++

Advertisements