Get Union between two List - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Get Union between two List

Demo Code


using System.Text;
using System.Collections.Generic;
using System;//from  www.  j a  v a2s.co  m

public class Main{
        public static List<T> GetUnion<T>(IList<T> list1, IList<T> list2)
        {
            SortedDictionary<T, int> result = new SortedDictionary<T, int>();
            foreach (T tmp in list1)
            {
                if (!result.ContainsKey(tmp))
                {
                    result.Add(tmp ,0);
                }
            }

            foreach (T tmp in list2)
            {
                if (!result.ContainsKey(tmp))
                {
                    result.Add(tmp, 0);
                }
            }

            return (List<T>)CollectionConverter.CopyAllToList<T>(result.Keys);
        }
}

Related Tutorials