Solved

Remove duplicates from Arraylist populated by Active Directory

Posted on 2004-08-05
2
4,823 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coding for the first time 9 104
Securing WEBAPI on Azure 2 57
c#, datatable, aspx 4 53
Unprotect Visio drawing page in C# 4 25
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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