Unfortunately, you did not provide a definition of
Chromosome
and did not explain what's is duplicated. I thing, this is a chromosome.
You need to keep all instances of the Chromosome in the container which guarantee uniqueness. It can be
Dictionary
(where a set of dictionary keys is unique)or
HashSet
. It the only feature you need is uniqueness, use
System.Collections.Generic.HashSet<Chromosome>
, see
http://msdn.microsoft.com/en-us/library/bb359438.aspx[
^].
To make you
Chromosome
class (I hope this is class, not structure, otherwise you should have used passing parameters by ref in the code you show) to be able to became an element of collection, you should make sure that its uniqueness is defined by its comparison operation. For this purpose, override
Equals(object)
and
GetHashCode
. Is's also good to define "==" and "!=" operators accordingly. Hash Code is the key to dictionaries and hash sets. Is does not have to be unique (it is not always possible) but "characteristic enough". On simple way of creating an acceptable hash function for a class or structure is this: calculate hash functions for all components and XOR them together.
[EDIT] Please see my other Answer in response to OP's new piece of code — corrections and notes.
—SA