Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

What I have tried:

expected result
Mango
 Guava
Orange
Kiwi
Posted
Updated 25-Jul-22 3:05am
v2

Simplest solution (that works for large data samples, rather than just the tiny ones you have there) is easy: Sort the two arrays.
Now each array is in order and a simple loop will enable you to see what is different.
Think about it:
A1) [W A M G B] => [A B G M W]
A2) [O K A W]   => [A K O W]

If you compare element 0 of A1 with element 0 of A2, then it's obvious A is duplicated - so move both indexes.
Compare element 1 of A1 with element 1 of A2 and it's obvious that B is unique - so move A1 index only
Compare element 2 of A1 with element 1 of A2 and it's obvious that G is unique - so move A1 index only
Compare element 3 of A1 with element 1 of A2 and it's obvious that K is unique - so move A2 index only
And so on until you run out of one array or the other, in which case all the rest of the items are unique.

Try it on paper and you'll see what I mean. .

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
v2
Comments
Maciej Los 25-Jul-22 6:52am    
5ed!
Dictionary<string,int>
Iterate the first list, adding strings with int 1.
Iterate the second list, adding new strings with int 1, adding 2 to the ints of existing entries.
Any item in the dictionary with int 3 is in both lists.

But I'd use a HashSet.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900