- 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
Counting the number of redundant characters in a string - JavaScript
We are required to write a JavaScript function that takes in a string and returns the count of redundant characters in the string.
For example − If the string is −
const str = 'abcde'
Then the output should be 0
If the string is −
const str = 'aaacbfsc';
Then the output should be 3
Example
Following is the code −
const str = 'aaacbfsc'; const countRedundant = str => { let count = 0; for(let i = 0; i < str.length; i++){ if(i === str.lastIndexOf(str[i])){ continue; }; count++; }; return count; }; console.log(countRedundant(str));
Output
Following is the output in the console −
3
Advertisements