Generic function to removeDuplicates from Generic List

        //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 @ Tuesday, September 11, 2007 3:42 PM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678