0

I would like to write a unit test to

  1. Go through a list of 1 million unique random generated strings.

  2. 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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200
  • 3
    Based on other questions you want to generate unique strings. Just because one set of 1 million is unique does not meant the next set of 1 million will be unique nor even the 1 million and 1 will be unique. I don't see how this can be a valid unit test. – paparazzo Jun 28 '15 at 18:34

3 Answers3

2

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.

Community
  • 1
  • 1
Matt Cole
  • 2,491
  • 17
  • 21
0

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.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
-1

If the objective is the best performance once the list is built, sort the list and check for duplicates.

Graffito
  • 1,658
  • 1
  • 11
  • 10