C# 配列同士で互いに存在するデータだけ抽出する

C#で、配列同士で互いに存在するデータだけ抽出するサンプルコードを記述してます。
環境
- OS windows10 pro 64bit
- Microsoft Visual Studio Community 2019 Version 16.7.1
互いに存在するデータだけ抽出
互いに存在するデータだけ抽出するには、「Intersect」を使用することで可能です。
int[] s1 = new int[] { 1, 2, 3, 4, 5 };
int[] s2 = new int[] { 1, 2, 3, 6 };
IEnumerable<int> s3 = s1.Intersect(s2); // 1,2,3
以下は、互いに存在するデータだけ抽出した結果を表示するだけのコードとなります。
using System;
using System.Linq;
using System.Collections.Generic;
namespace testapp
{
class Program
{
static void Main(string[] args)
{
int[] s1 = new int[] { 1, 2, 3, 4, 5 };
int[] s2 = new int[] { 1, 2, 3, 6 };
IEnumerable<int> s3 = s1.Intersect(s2);
Console.WriteLine(String.Join(", ", s3.Select(i => i)));
// 入力待ち
System.Console.ReadKey();
}
}
}
実行結果

-
前の記事
dockerコンテナのAlpine Linuxのタイムゾーンを設定する 2021.12.03
-
次の記事
Linux wgetでファイル名を指定してダウンロードする 2021.12.03
コメントを書く