| Viewed 61 times

How to Count the Number of Duplicate Characters in JavaScript?


I need to create a function that returns the number of duplicate characters in a string. It will be case sensitive and spaces are included. If there are no duplicates, return 0.

1 Answer
Mohammad Qandeel
Answered
24
const duplicates = str => str.length - new Set(str).size;

Example

duplicates('1100') // 2



Related Questions