Hi all,
I have an array that contains email addresses, before inserting a new address which are being read from a text file into the array how can I check that the address doesn't already exist in the array? I'm trying to make sure there are no duplicates in the array, rather than sorting the array once they are all in, can I check before inserting the address?
Many thanks
PS Using .net 2.0
J
Loading
Satish KathiPosted Jul 22, 2008, 10:12 AM
Here is code for using array List
ArrayList email = new ArrayList();
if (!email.Contains(your email string read from file))
{
email.Add(your email string read from file);
}
If you are using dictionary
Dictionary<string, string> email2 = new Dictionary<string, string>();
if (!email2.ContainsKey(your email string read from file))
{
email2.Add(your email string read from file, your email string read from file);
}
Dictionary is a collection of key, value pair. Key must be unique. Retrieving the value is very fast as it is implemented as a hash table.
Niradhip ChakrabortyPosted Jul 21, 2008, 7:37 PM
ArrayList EmailAddresses=new ArrayList();
Dictionary dictionary = new Dictionary();
foreach (string field in fields)
{
if (dictionary.ContainsKey(field))
{
//duplicate found!
}
else
{
int value = 0; //not used but required by Dictionary
dictionary.Add(field,value);
}
}
But what I suggest you to use hash table where you can use the key-value pair to eliminate the duplicate e-mail address.