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.
0— no characters repeats more than once
2— ‘a’ and ‘b’
2— ‘a’ occurs twice and ‘b’ twice (b and B)
My Thought Process Before Sets
- First converting the text passed into the function to a uniform letter casing, then …
Loop over the string, making each character a property of the empty object created above. This is a two-step process being that …
If the character doesn’t exist on the object, it is added as a new property and its value set to 1.
If, however, the character already exists on the object, its value is incremented by 1.
After completely iterating through the string, I’ll then iterate over the object and push properties which have values > 1 to an array which I’d call ‘dupes’.
After iterating over the object, I’d then return the length of the dupes array, since that would contain all duplicates present in the provided string.
Here’s a valid implementation of what this idea would look like:
My Thought Process After Sets
However, since learning that “the Set object lets one store unique values of any type”, this is how I went about the challenge.
- Create two sets, one for all the characters in the provided string (tracker) and another for characters which appear more than once (dupes).
Iterate over a uniform-cased version of the provided string checking if the tracker has the current character.
If true, dump it in the dupes set, otherwise, add it to the tracker set.
After iteration, simply return the size of the dupes set since every character dumped there would only appear once. That is, if during iteration say for a string like “For loop” which has 3 ‘o’ characters, instead of adding two ‘o’s to the dupes set, only one ‘o’ would be added as sets can only contain unique values.
Here’s a working implementation of this idea: