
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- Counting number of vowels in a string with JavaScript
- Keeping only redundant words in a string in JavaScript
- Number of non-unique characters in a string in JavaScript
- Counting the number of palindromes that can be constructed from a string in JavaScript
- Counting number of characters in text file using java
- Number of letters in the counting JavaScript
- Counting divisors of a number using JavaScript
- Counting number of words in a sentence in JavaScript
- Regrouping characters of a string in JavaScript
- Counting the number of 1s upto n in JavaScript
- Counting number of 9s encountered while counting up to n in JavaScript
- Reverse the words in the string that have an odd number of characters in JavaScript
- Counting all possible palindromic subsequence within a string in JavaScript
- Count the Number of matching characters in a pair of Java string
- Counting number of triangle sides in an array in JavaScript

Advertisements