8

I would like to know an efficient method to remove duplicate items from a string array in C#.

For example,

string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };

will become,

string[] a = { "abc", "xyz","def", "ghi", "asdf", "xd" };

How to fill the gaps after removing the duplicate entries? Is there a way to do this without using an extra array for storing the elements?

Method which I used:

1) Sorted the array

2) Replaced the duplicate entries with null

3) Copied NOT null string to a new array.

But looking for an optimized way to doing the same.

EDIT: I am using .NET 2.0 and VS 2005

4 Answers 4

16

You can use a HashSet:

string[] a = { "abc", "xyz","abc", "def", "ghi", "asdf", "ghi","xd", "abc" };
var b = new HashSet<string>(a);
Sign up to request clarification or add additional context in comments.

1 Comment

Can I use var in VS 2005 with .Net 2.0 ?
10

You can't resize an array in .NET, so whatever way you use to remove the duplicates, you have to create a new array for the result.

You can use a HashSet<string> to easily remove the duplicates:

a = new HashSet<string>(a).ToArray();

The hash set will add the items from the array to itself, and automatically discard the duplicates. As the hash set uses hash codes to check for existing items, this will be somewhat faster than sorting the items, however the result is of course not sorted.

2 Comments

I installed .Net 3.5 update. But I get the error, "'System.Collections.Generic.HashSet<string>' does not contain a definition for 'ToArray'".
@pragadheesh: The ToArray is not a method in the class itself, it's an extension method. You need using System.Linq; at the top of the file to get the extension methods.
7

Take a look at IEnumerable.Distinct() Method

Comments

6

If using .NET 3.0 you can use LINQ:

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] a = { "abc", "xyz", "abc", "def", "ghi", "asdf", "ghi", "xd", "abc" };
            string[] b = a.Distinct().ToArray();
            foreach (string s in b)
                Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.