C# List集合中獲取重復值及集合運算


一、獲取集合內重復值

 

public void GetDuplicateValue()
{
    List<string> lisA = new List<string> { "A", "B", "C", "A" };
    //方式一 借助字典
    Dictionary<string, int> dic = new Dictionary<string, int>();
    lisA.ForEach(x =>
    {
        if (dic.ContainsKey(x))
            dic[x] += 1;
        else
            dic[x] = 0;
    });
    List<string> lisDupValues = dic.Where(x => x.Value > 0).Select(x => x.Key).ToList();  //結果{"A"}
 
    //方式二
    List<string> lisDupValues2 = lisA.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); //結果{"A"}
 
    //方式三 等同於方式二
    List<string> lisDupValues3 = (from r in lisA group r by r into g where g.Count() > 1 select g.Key).ToList(); //結果{"A"}
}

 

 二、單個集合操作

1、All、Any

    public void All_Any()
    {
        List<string> lisA = new List<string> { "A", "B", "C", "A" };
        //All:確定序列中的所有元素是否都滿足條件
        bool all = lisA.All(x => x.Equals("B")); //結果 false
     
        //Any:確定序列中的任何元素是否存在或滿足條件。
        bool any = lisA.Any(x => x.Equals("B"));  //結果 true
    }

 2、Sum、Average、Distinct、Max、Min、Select、Skip、Take、ToDictionary

public void Sum_Average_Distinct_Max_Min_Skip_Take_ToDictionary()
{
    List<int> lisA = new List<int> { 1, 2, 2, 3 };
 
    //Sum:計算數值序列的和。
    double sum = lisA.Sum();  //結果 8
 
    //Average:計算數值序列的平均值。
    double average = lisA.Average();  //結果 2
 
    //Distinct:返回序列中的非重復元素
    List<int> distinctLisA = lisA.Distinct().ToList();  //結果 {1,2,3}
 
    //Max:返回值序列中的最大值。
    double max = lisA.Max();  //結果 3
 
    //Min:返回值序列中的最小值。
    double min = lisA.Min(); //結果 1
 
    //Select:將序列中的每個元素投影到新表單。
    var query = lisA.Select((age, index) => new { index, jn = age + 1 }); //結果:{index=0,jn=2},{index=1,jn=3},{index=2,jn=3},{index=3,jn=4}
 
    //Skip:跳過序列中指定數量的元素,然后返回剩余的元素。
    List<int> lowerGrades = lisA.Skip(3).ToList();  //結果 {3}
 
    //Take:從序列的開頭返回指定數量的相鄰元素。
    List<int> task = lisA.Take(2).ToList();  //結果 {1,2}
 
    //ToDictionary:根據指定的鍵選擇器函數、比較器和元素選擇器函數,從 IEnumerable<T> 創建一個 Dictionary<TKey,TValue>。
    var dic = lisA.Distinct().ToDictionary(x => x); //結果 {{1,1},{2,2},{3,3}}
}

 三、集合間運算

1、Concat、Except、Intersect、Union、Zip

public void Concat_Except_Intersect_Union_Zip()
{
    List<string> lisA = new List<string> { "A", "B", "C", "A" };
    List<string> lisB = new List<string> { "A", "B", "H", "K" };
 
    //Concat:連接兩個序列。
    List<string> query = lisA.Concat(lisB).ToList();  //結果 { "A", "B", "C", "A" ,"A", "B", "H", "K"}
 
    //Except:生成兩個序列的差集。
    List<string> onlyInLisASet = lisA.Except(lisB).ToList();   //結果 {"C"}
 
    //Intersect:生成兩個序列的交集。
    List<string> duplicates = lisA.Intersect(lisB).ToList();  //結果 {"A","B"}
 
    //Union:生成兩個序列的並集。
 
    List<string> union = lisA.Union(lisB).ToList();  //結果 { "A", "B", "C", "H", "K"}
 
    //Zip:將指定函數應用於兩個序列的對應元素,以生成結果序列。
        List<string> zip=lisA.Zip(lisB, (first, second) => first + " " + second).ToList(); //結果 { "A A", "B B", "C H", "A K" }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM