Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# เพื่อคืนค่าความแตกต่างระหว่างสองลำดับ


ตั้งค่าสองซีเควนซ์

double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };
double[] arr2 = { 15.6, 30.5, 50.2 };

หากต้องการทราบความแตกต่างระหว่างอาร์เรย์ทั้งสองข้างต้น ให้ใช้วิธีการยกเว้น ()

IEnumerable<double> res = arr1.AsQueryable().Except(arr2);

ต่อไปนี้เป็นรหัสที่สมบูรณ์

ตัวอย่าง

using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
   static void Main() {
      double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };
      double[] arr2 = { 15.6, 30.5, 50.2 };
      Console.WriteLine("Initial List...");
      foreach(double ele in arr1) {
         Console.WriteLine(ele);
      }
      IEnumerable<double> res = arr1.AsQueryable().Except(arr2);
      Console.WriteLine("New List...");
      foreach (double a in res) {
         Console.WriteLine(a);
      }
   }
}

ผลลัพธ์

Initial List...
10.2
15.6
23.3
30.5
50.2
New List...
10.2
23.3