日期:2014-05-18 浏览次数:21389 次
public static ArrayList DedupStringList(ArrayList list) { if (list == null) { return new ArrayList(0); } ArrayList result = new ArrayList(); Dictionary<string, string> hashset = new Dictionary<string, string>(); foreach (string str in list) { if (string.IsNullOrEmpty(str.Trim())) { continue; } string s = str.Trim().ToLower(); if (!hashset.ContainsKey(s)) { hashset.Add(s, string.Empty); result.Add(s); } } result.TrimToSize(); return result; }
------解决方案--------------------
Contains 就只这样
ArrayList result = new ArrayList() { 1, 2, 3, 3 };
ArrayList result2 = new ArrayList();
foreach (var r in result)
{
if (!result2.Contains(r)) result2.Add(r);
}
------解决方案--------------------
ArrayList result1 = new ArrayList() { 1, 2, 3, 4, 5, 6 };
ArrayList result2 = new ArrayList() { 5, 6, 7, 8, 9, 10 };
result1.AddRange(result2);
var source = result1.Cast<int>().Distinct().ToArray();
------解决方案--------------------
public static void Main(string[] args) { int[] A = { 1, 2, 2, 4, 5, 3 }; int[] B = { 2, 4, 5, 7, 5, 6, 8 }; //1.1, 合并以上两个数组,合并结果不包含重复项,并排序。 //要求结果为: 1,2,3,4,5,6,7,8 A.Concat(B).Distinct().OrderBy(i => i).ToList().ForEach(i => Console.Write(i.ToString())); Console.WriteLine(Environment.NewLine); //2,去除以上两个数组中所有重复项,并合并,并排序。 //要求结果为: 1,3,6,7,8 A.Except(B).Concat(B.Except(A)).OrderBy(i => i).ToList().ForEach(i => Console.Write(i.ToString())); }