Code Snippet: Remove Duplicates From ArrayList

Two methods to remove Duplicates from ArrayList.

Method 1:

private static ArrayList RemoveDuplicates(ArrayList arrList) 
{ 
        ArrayList list = new ArrayList();
        foreach (string item in arrList) 
        { 
             if (!list.Contains(item)) 
            { 
                list.Add(item); 
            } 
        } 
        return list; 
}

Method 2:

private static string[] RemoveDuplicates(ArrayList arrList)
{
        HashSet<string> Hset = new HashSet<string>((string[])arrList.ToArray(typeof(string)));
        string[] Result = new string[Hset.Count];
        Hset.CopyTo(Result);
        return Result;
}

Note: I found this method on the net and therefore I am sharing it as it is. Use any of the above which suites your requirement.

comments powered by Disqus