Code Wars: Counting Duplicates

Count the number of Duplicates

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

Example

"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice

function duplicateCount(text){
  let arr = text.split("").map(item => item.toLowerCase());    //can lowercase when it's a string first!
  let arrCount = [];
  
  for (i = 0; i < arr.length; i++) {
    if(arr.indexOf(arr[i], i + 1) !== -1 && arrCount.includes(arr[i]) === false) {
      arrCount.push(arr[i]);
    }
  }
  return arrCount.length;
}

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int