Removing duplicate entries from a string

I had a string whose chances of having duplicate items was rather high. But, I needed to reduce that change to zero. I came across Ali Raza’s site, and he had a solution for VB.NET. It uses an ArrayList, and my preferred weapon of choice is a List. My question was how to join items in the List into one big string. I came across dotnetperls for that piece of the puzzle, and here is the final bunch:

// existingIds is semicolon-delimited string
string[] list = existingIds.Split(';');
List<string> duplicates = new List<string>() { };
for (int i = 0; i < list.Length; i++)
{
     if (!duplicates.Contains(list[i]))
     duplicates.Add(list[i]);
}
Session["RecentDocs"] = string.Join(";", duplicates.ToArray());
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: