- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
JavaScript Remove non-duplicate characters from string
We are required to write a JavaScript function that takes in a string and returns a new string with all non-duplicate characters removed from it.
For example −
If the input string is −
"teeth_foot"
Then the output should be −
"teetoot"
Therefore, let's write the code for this function −
Example
const str = 'teeth_foot'; const removeNonDuplicate = str => { const strArray = str.split(""); const duplicateArray = strArray.filter(el => { return strArray.indexOf(el) !== strArray.lastIndexOf(el); }); return duplicateArray.join(""); }; console.log(removeNonDuplicate(str));
Output
The output in the console will be −
teetoot
Advertisements