
- 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
Number of non-unique characters in a string in 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
The code for this will be −
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
The output in the console will be −
3
- Related Articles
- How to find unique characters of a string in JavaScript?
- Constructing array from string unique characters in JavaScript
- Filtering string to contain unique characters in JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- Mapping unique characters of string to an array - JavaScript
- JavaScript Remove non-duplicate characters from string
- Counting the number of redundant characters in a string - JavaScript
- Checking if a string contains all unique characters using JavaScript
- Maximum Length of a Concatenated String with Unique Characters in C++
- How to find the number of occurrences of unique and repeated characters in a string vector in R?
- Remove all non-alphabetical characters of a String in Java?
- Count Unique Characters of All Substrings of a Given String in C++
- Regrouping characters of a string in JavaScript
- Program to count number of unique palindromes we can make using string characters in Python
- Program to find length of concatenated string of unique characters in Python?

Advertisements