I recently found a question that went something like:
"Given an array of strings, return the number of distinct strings in that array."
I came up with this solution:
1. Get number_of_strings, which equals the number of strings in the input array
2. Get number_of_non_redundant, which equals the length of the input array cast as a set
3. Return 2 times number_of_non_redundant - number_of_strings
So, my question is, does this algorithm work for all data sets?
As others have pointed out, simply returning
number_of_non_redundantseems like the answer to this problem.Here is a possible solution for determining
number_of_non_redundant:Using a Hash Set here offers constant time operations(add, contains).
Additionally I wanted to point out that you can’t (at least I am not aware of this in a language) simply cast an array to a set. Casting is a constant time operation. These are two different data structures and in order to take elements from an array and place them in a set, it would require iterating through the array and entering the elements into a set.