Home > DeveloperSection > Forums > Remove duplicates from array
Ankit Singh

Total Post:341

Points:2389
Posted on    February-16-2013 8:14 AM

 C# C# 
Ratings:


 2 Reply(s)
 1190  View(s)
Rate this:

Hi Everyone!

I have been working with a string[] array in C# that gets returned from a function call. I was wondering what the best way to remove duplicates from this

array would be? I could possibly cast to a Generic collection, but I was wondering if there was a better way to do it, possibly by using a temp array?

Thanks in advance!



Shankar M

Total Post:25

Points:175
Posted on    February-17-2013 9:55 PM

Hi Ankit,

You can remove Duplicates by using a HashSet.

string[] names = {"Windows", "Oracle", "Windows", "Csharp", "Oracle", "Windows", "Telnet", "Oracle", "Csharp" };
            var AlteredSet = new HashSet<string>(names);

            foreach (string str in AlteredSet)
            {
                MessageBox.Show(str);
            }

Regards,
Shankar



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    February-18-2013 1:31 AM

Hi Ankit!

You could try this way!

int[] nAs = { 1, 2, 3, 3, 4}; 
int[] nQ = nAs.Distinct().ToArray();

Modified On Feb-18-2013 01:33:19 AM

Don't want to miss updates? Please click the below button!

Follow MindStick