iT邦幫忙

0

有兩個陣列分別裝字串形式的數字,要看裡面共有幾組一樣的數字

  • 分享至 

  • xImage

例如:{"1","7","90","66","5"}和{"90","8","7","95"}要計算共有幾組一樣的數字
凡請各位前輩不吝賜教!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
緯大啊緯大人
iT邦研究生 1 級 ‧ 2021-12-14 08:26:41

不重複的情況下
不考慮效能的情況下

foreach 陣列1
{
  foreach 陣列2
  {
    if(陣列1 的字串 等於 陣列2的字串)
    {
      count++;
    }
  }
}

其它自己想像吧,好像也沒有其他了.....?

0
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();
    }

https://ithelp.ithome.com.tw/upload/images/20211214/20135969jWjNcZv97l.jpg

2
海綿寶寶
iT邦大神 1 級 ‧ 2021-12-14 09:50:31

https://ithelp.ithome.com.tw/upload/images/20211214/20001787cvoxoWn0WE.png

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);
    }
}

我要發表回答

立即登入回答