It's an exercise, which I found on Codewars.
Instructions:
Write a function which returns the count of distinct case-insensitive alphabetic characters and numeric digits which occur more then once in a given string. The given string can be assumed to contain only alphabets (lower-, uppercase) and numerics.
Examples:
"abcde" results in 0, because no characters repeats more than once.
"aabbcde" results in 2, because of 'a' and 'b'.
"aabBcde" => 2, because 'a' occurs twice and 'b' occurs twice (b and B).
"indivisibility" => 1, because 'i' occurs six times.
"Indivisibilities" => 2, because 'i' seven times & 's' twice.
"aA11" => 2, because 'a' and '1'.
"ABBA" -> 2, because 'A' and 'B' each twice.
My (valid*) solution:
fun duplicateCount(text: String): Int {
var count = 0
var invalid = ArrayList<Char>()
var i = 0
while (i < text.length) {
if (invalid.contains(text[i].toLowerCase())) {
i++
continue
}
var j = i + 1
while (j < text.length) {
if (text[i].toLowerCase() == text[j].toLowerCase()) {
invalid.add(text[i].toLowerCase())
count++
break
}
j++
}
i++
}
return count
}
*It has passed the unit-tests.
What are your thoughts about my implementation?
How could it be improved? What would you have done differently and why?
Looking forward to reading your comments and answers.