Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

        //created based on http://www.kirupa.com/net/removingDuplicates2.htm

        //Why it is not in .Net Framework yet? Why HashSet<T> is only in Orcas(http://blogs.msdn.com/bclteam/archive/2006/11/09/introducing-hashset-t-kim-hamilton.aspx)

        public static List<GenericType> removeDuplicates<GenericType>(List<GenericType> inputList)

        {

            Dictionary<GenericType, int> uniqueStore = new Dictionary<GenericType, int>();

            List<GenericType> finalList = new List<GenericType>();

 

            foreach (GenericType currValue in inputList)

            {

                if (!uniqueStore.ContainsKey(currValue))

                {

                    uniqueStore.Add(currValue, 0);

                    finalList.Add(currValue);

                }

            }

            return finalList;

        }

 

Alternative solution is to use List.RemoveAll(Predicate<T>) -(misleading name for the function, that allow to remove only specified by Predicate)

 

Posted on Tuesday, September 11, 2007 3:42 PM | Back to top


Comments on this post: Generic function to removeDuplicates from Generic List

# re: Generic function to removeDuplicates from Generic List
Requesting Gravatar...
In 3.5 the function is obsolete, use LINQ Distinct() instead
Left by Michael Freidgeim on Jul 26, 2009 2:17 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free