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

โปรแกรม C# หาความแตกต่างระหว่างสองลำดับข้อมูล

กำหนดสองลำดับข้อมูล:

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

เพื่อหาความแตกต่างระหว่างอาร์เรย์ทั้งสอง ให้ใช้เมธอด Except()

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