×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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
Mango
Guava
Orange
Kiwi

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

Banana should be in expected result too

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

(OP)
yes
correct
sorry for missed Banana

expected result will be

expected result
Mango
Guava
Banana
Orange
Kiwi

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

(OP)
so How to do that please ?

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

What have you tried so far ?

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

(OP)
i tried and reached to result by linq

var array1 = new[] { "Watermelon", "Apple", "Mango", "Guava", "Banana" };
var array2 = new[] { "Orange", "Kiwi", "Apple", "Watermelon" };

var result = array2.Except(array1).Concat(array1.Except(array2));

but i need it to get result without using linq or set operator

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

Simply something like this should work:
iterate over first array and check every element if it is in the second array too - and if not then this is the different element.
Then do the same with the second array.

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

I tried what I described before:

CODE

using System;

class Program
{
	public static void Main(string[] args)
	{
		string[] a = {"Watermelon", "Apple", "Mango", "Guava", "Banana"};
		string[] b = {"Orange", "Kiwi", "Apple", "Watermelon"};

		Console.WriteLine("Differences:\n");	
		int numDiff = arrayDifference(a, b);
		string diffResult = numDiff == 0 ? "There are no differences" : "Number of differences found: " + numDiff;
		Console.WriteLine("\n" + diffResult + "\n");
		
		Console.Write("Press any key to continue . . . ");
		Console.ReadKey(true);
	}
	
	static int arrayDifference(string[] x, string[] y) {
		int numberOfDifferentElements = 0;
		
		foreach(string s in x) {
			if (Array.IndexOf(y, s) == -1) {
				Console.WriteLine(s);
				numberOfDifferentElements++;
			}
		}
		
		foreach(string s in y) {
			if (Array.IndexOf(x, s) == -1) {
				Console.WriteLine(s);
				numberOfDifferentElements++;
			}
		}
		
		return numberOfDifferentElements;
	}
} 

Output:

CODE

Differences:

Mango
Guava
Banana
Orange
Kiwi

Number of differences found: 5

Press any key to continue . . . 

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

ahmedsa2018, was this what you needed ?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close