question

ahmedsalah-1628 avatar image
0 Votes"
ahmedsalah-1628 asked JackJJun-MSFT commented

How to get difference between two array without using linq or Set operator using csharp ?

I work on csharp
I have two arrays of string

A1 = [Watermelon, Apple, Mango, Guava, Banana]
A2 = [Orange, Kiwi, Apple, Watermelon]

i need to write code by csharp get difference between two arrays
and display difference between two arrays but without using linq or set operator

expected result

224219-image.png


dotnet-csharpdotnet-runtimedotnet-standard
image.png (1.6 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

i try with this

var result = A2.Except(A1).Concat(A1.Except(A2));
but this is linq
can you help me with solution without using linq or set operator

0 Votes 0 ·
ShuaiHuaDu avatar image ShuaiHuaDu ahmedsalah-1628 ·

Please see my answer, it used a normal way, but it is not a simple way.

0 Votes 0 ·

@ahmedsalah-1628, is any update? Do you have time to check if my answer works for you?

0 Votes 0 ·
ShuaiHuaDu avatar image
0 Votes"
ShuaiHuaDu answered ShuaiHuaDu commented

Hi, please refer the code below:

 namespace MSTestProject  
 {  
     [TestClass]  
     public class UnitTest1  
     {  
         [TestMethod]
         public void Test()
         {
             var list = GetListDifference();
             foreach (var item in list)  
             {  
                 Console.WriteLine(item);  
             }  
         }
  
         public List<string> GetListDifference()  
         {  
             var list1 = new List<string> { "Watermelon", "Apple", "Mango", "Guava", "Banana" };  
             var list2 = new List<string> { "Orange", "Kiwi", "Apple", "Watermelon" };  
      
             var elements = new List<string>();  
      
             foreach (var item1 in list1)  
             {  
                 foreach (var item2 in list2)  
                 {  
                     if (item1 == item2)  
                     {  
                         elements.Add(item1);  
                     }  
                 }  
             }  
      
             if (elements.Count > 0)  
             {  
                 foreach (var item in elements)  
                 {  
                     list1.Remove(item);  
                     list2.Remove(item);  
                 }  
             }  
      
             var result = new List<string>();  

             result.AddRange(list1); 
             result.AddRange(list2); 

             return result;
         }  
     }  
 }  


If right, please Accept.
Enjoy programming!!!

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

but concate is linq
var result = list1.Concat(list2);
so how to handle that

alsohow to append items and display it outside foreach

 foreach (var item in result)
              {
                  Console.WriteLine(item);
              }


0 Votes 0 ·
ShuaiHuaDu avatar image ShuaiHuaDu ahmedsalah-1628 ·

I modified the code, remove the reference of Contact method please check.

0 Votes 0 ·
ShuaiHuaDu avatar image ShuaiHuaDu ahmedsalah-1628 ·

You can make the result as a return value in a method that it return List<string>.

0 Votes 0 ·
ShuaiHuaDu avatar image ShuaiHuaDu ahmedsalah-1628 ·

I modified the code again, please check.

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

not sure why Banana was not included (where you supposed to only compare by min array size)

with banana

 static string[] Diff(string[] list1, string[] list2)
 {
     var diff = new List<string>();
     foreach (var i in list1)
     {
         if (Array.IndexOf(list2, i) < 0) diff.Add(i);  
     }
     foreach (var i in list2)
     {
         if (Array.IndexOf(list1, i) < 0) diff.Add(i);  
     }
     return diff.ToArray();
 }

without Banana

 static string[] Diff(string[] list1, string[] list2)
 {
     var diff = new List<string>();
     var minSize = Math.Min(list1.Length, list2.Length);
        
     for (var i=0; i < minSize; ++i)
     {
         if (Array.IndexOf(list2, list1[i]) < 0) diff.Add(list1[i]);  
     }
     for (var i=0; i < minSize; ++i)
     {
         if (Array.IndexOf(list1, list2[i]) < 0) diff.Add(list2[i]);  
     }
    
     return diff.ToArray();
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@ahmedsalah-1628, Welcome to Microsoft Q&A, you could refer to the following code to get difference between two array without using linq.

Code:

   static void Main(string[] args)
         {
             string[] arr1 = { "Watermelon", "Apple", "Mango", "Guava", "Banana" };
             string[] arr2 = { "Orange", "Kiwi", "Apple", "Watermelon" };
             List<string> result = new List<string>();
             GetList(arr1, arr2, result);
             GetList(arr2, arr1, result);
             foreach (var item in result)
             {
                 Console.WriteLine(item);
             }
             Console.WriteLine();
         }
         static void GetList(string[] arr1, string[] arr2,List<string> result)
         {
             foreach (string tmp in arr1)
             {
                 bool existsInB = false;
                 foreach (string tmp2 in arr2)
                 {
                     if (tmp == tmp2)
                     {
                         existsInB = true;
                         break;
                     }
                 }
    
                 if (!existsInB)
                 {
                     result.Add(tmp);
                 }
             }
    
         }

Result:
224669-image.png



Hope the above code could help you.

Best Regards,
Jack


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


image.png (2.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.