REMOVE DUPLICATES FROM ARRAY

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C# 
C#
 1455  View(s)
Ratings:
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!

  1. Shankar M

    Post:25

    Points:175
    Re: Remove duplicates from array

    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


  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Remove duplicates from array

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!