C# 中的取交集方法


使用 Intesect 方法獲取公共元素 −

建立列表 −

var list1 = new List{99, 87};
var list2 = new List{56, 87, 45, 99};

現在,使用 Intersect() 方法從上述列表中獲取公共元素 −

list1.Intersect(list2);

以下是完整程式碼 −

示例

 即時演示

using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
   public static void Main() {
   
      // two lists
      var list1 = new List<int>{99, 87};
      var list2 = new List<int>{56, 87, 45, 99};

      // common values
      var res = list1.Intersect(list2);
      foreach(int i in res) {
         Console.WriteLine(i);
      }
   }
}

輸出

99
87

更新於: 2020-06-22

390 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.