I would like to write a unit test to
Go through a list of 1 million unique random generated strings.
Ensure that each number is 100% unique and there are no duplicates.
What is the best way to check and compare that there are no duplicates?
I would like to write a unit test to
Go through a list of 1 million unique random generated strings.
Ensure that each number is 100% unique and there are no duplicates.
What is the best way to check and compare that there are no duplicates?
You could use the LINQ Distinct()
method. It is likely to use hashing, and be close in performance to anything hand rolled. Some information on performance here.
When you refer to a "unit test," you usually mean a test which you will run routinely to ensure that your code continues to work correctly even after you have changed some of it. What you are asking for in this question seems to be more a matter of verifying that some set of strings is in fact unique, not that the code which generates them is correctly ensuring uniqueness. What would be better from a programming perspective is a unit test which verifies that the code which ensures uniqueness is working. That should probably be a method along the lines of, let's say, wouldBeUniqueInSet(String newString, Set<String> existingSet)
To test that that method is working correctly, you would want to run it with a string that is already in the set and expect it to return false
, and with a set that is not already in the set and expect it to return true
. Those are unit tests in the sense which we usually use the term.
If the objective is the best performance once the list is built, sort the list and check for duplicates.