Solved

Remove duplicates from Arraylist populated by Active Directory

Posted on 2004-08-05
2
4,794 Views
Last Modified: 2011-10-03
Over here, I've got a problem with out Exchange 2003 Server, probably during migration from 5.5 -> 2003 some mail addresses were duplicated with different CNs.
The following code should sort a list of email adresses, which I recieve from our AD (contacts.Add(contact.Properties["mail"].Value.ToString());) and print out duplicated ones.

#####
contacts.Sort();
for (int i=1; i <= contacts.Count-1; i++)
{
      Console.WriteLine(contacts[i]);
      Console.WriteLine(contacts[i-1]);
      if(contacts[i] == contacts[i-1])
      {
            Console.WriteLine("Duplicate: "+contacts[i]);
      }
}
#####

As you can see, I'm outputting the email addresses it's going to check, and yes, they are the same - of course. I'm searching for the exact email adresse with objSearcher.Filter = "(mail=frank.lattuca@saarland.ikk.de)";

However the if condition returns false.
Now, if I'd check if the .GetHashCode() of contacts[x] are the same, it returns true.

Am I save to use the hash for comparing?
Why aren't the equal strings equal?

FYI: contacts is a System.Collections.ArrayList
0
Comment
Question by:pYrania
2 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 500 total points
ID: 11725461
I believe the problem is because you are comparing the objects, not the values of the string.

Try this.

contacts.Sort();
for (int i=1; i <= contacts.Count-1; i++)
{
     Console.WriteLine(contacts[i]);
     Console.WriteLine(contacts[i-1]);
     if(contacts[i].ToString() == contacts[i-1].ToString())
     {
          Console.WriteLine("Duplicate: "+contacts[i]);
     }
}
0
 
LVL 3

Author Comment

by:pYrania
ID: 11725532
You are the man.

6 duplicates out of 998 contacts, not that bad. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now