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

ahmed salah 3,216 Reputation points
2022-07-25T09:50:44.703+00:00

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

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,346 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} votes

3 answers

Sort by: Most helpful
  1. ShuaiHua Du 636 Reputation points
    2022-07-25T10:32:07.597+00:00

    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!!!


  2. Bruce (SqlWork.com) 55,041 Reputation points
    2022-07-25T22:00:54.9+00:00

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

  3. Jack J Jun 24,281 Reputation points Microsoft Vendor
    2022-07-26T07:35:26.15+00:00

    @ahmed salah , 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.

    0 comments No comments