例如:{"1","7","90","66","5"}和{"90","8","7","95"}要計算共有幾組一樣的數字
凡請各位前輩不吝賜教!
不重複的情況下
不考慮效能的情況下
foreach 陣列1
{
foreach 陣列2
{
if(陣列1 的字串 等於 陣列2的字串)
{
count++;
}
}
}
其它自己想像吧,好像也沒有其他了.....?
using System.Linq;
public void Intersect()
{
var List_A = new int[] { 1, 7, 90, 66, 5 };
var List_B = new int[] { 90, 8, 7, 95 };
var result = List_A.Where(x => List_B.Contains(x)).ToArray();
}
using System.IO;
using System;
class Program {
static void Main() {
var arr1 = new string[] { "1", "7", "90", "66", "5" };
var arr2 = new string[] { "90", "8", "7", "95" };
var count = 0;
for (int i=0; i<arr1.GetLength(0); i++) {
for (int j=0; j<arr2.GetLength(0); j++) {
if (arr1[i]==arr2[j]) {
Console.WriteLine("Same : {0}", arr1[i]);
count = count + 1;
break;
}
}
}
Console.WriteLine("Count = {0}", count);
}
}