1

## 兩個陣列做值的比較，把陣列內不一樣的值挑出來

a、b型態都是string，但我裡面存的都是數字

### 5 個回答

0

iT邦大師 1 級 ‧ 2019-06-01 22:22:31

1

iT邦大神 1 級 ‧ 2019-06-01 23:07:31

``````using System;
using System.Linq;

public class Program
{
public static void Main()
{
string[] a = { "100", "200", "300" , "400" , "500"  };
string[] b = { "100", "222", "300" , "444"};
foreach (string str in b)
{
if (a.Contains(str)) {
Console.WriteLine("Yes-" + str );
} else {
Console.WriteLine("No-" + str);
}
} // 1
}
}
``````
1

iT邦新手 5 級 ‧ 2019-06-02 00:12:37

2

iT邦高手 1 級 ‧ 2019-06-02 01:49:10

=>換個邏輯想，就是以B為主，排除A有出現的，剩下的就是與A不一樣的

``````double[] B = { 2.0, 2.0, 2.1, 2.2, 2.3, 2.3, 2.4, 2.5 };
double[] A = { 2.2 };

IEnumerable<double> different = B.Except(A);

foreach (double number in different)
Console.WriteLine(number);

/*

2
2.1
2.3
2.4
2.5
*/
``````
0
PotRookie
iT邦新手 5 級 ‧ 2019-06-02 16:44:20

``````string[] a = { "1", "2", "3", "4", "5" };
string[] b = {"2", "3", "4", "5", "6", "7", "8", "0"};
var a2 = new HashSet<string>(a);

foreach (var val in b)
{
if (!a2.Contains(val))
{
Console.WriteLine(val);
}
}
//6 7 8 0
``````