Link to home
Start Free TrialLog in
Avatar of dominicwong
dominicwong

asked on

How to compare two lists of integers in c#

Hi experts
I'd some research on comparing two lists of integers but can't find the things I need.

I have two lists of integers. User will only have the old list.
I would like to determine a course of actions that instructs a user how to produce the new list from the old list.

eg.
old list:  1 2 3 4
new list:5 7 2 3 4

What I need are actions such as:
1) Remove item "1" from index 0
2) Add item "5" to index 0
3) Add item "7" to index 1

Thanks in advance

PS. I am using .NET 4.0
Avatar of HainKurt
HainKurt
Flag of Canada image

here

		List<int> iList = new List<int>()
		{1, 2, 3, 4};

/*
What I need are actions such as:
1) Remove item "1" from index 0
2) Add item "5" to index 0
3) Add item "7" to index 1
*/
		iList.Remove(1);
		iList.Insert(0, 5);
		iList.Insert(1, 7);

		foreach (int i in iList)
		{
			Console.WriteLine(i);
		}

Open in new window

result
5
7
2
3
4

Open in new window


https://dotnetfiddle.net/Bg0WmA
Avatar of dominicwong
dominicwong

ASKER

Thanks HainKurt.
I am afraid that wasn't my question.

>> I would like to determine a course of actions that instructs a user how to produce the new list from the old list.
What I need is how to analyze the two lists and produce the course of actions.
With the course of actions, a user can take the original list and come up with a new list.
I dont get what you need...

can you give any example?
An example is, I have two lists:
list A:  1, 2, 3, 4
list B:  5, 7, 2, 3, 4

Presumably, a user has knowledge of list A, i.e. 1, 2, 3, 4
If I want to inform the user about the content of list B, I could either give him the entire new list B.
Or, I could simply tell him to replace the first number "1" in his list by "5" and "7".

My question is how my C# program be able to analyze and compare the two lists A and B, and come up with the instruction: replace the first number "1" by "5" and "7".

Normal LINQ 'Except' or 'Intercept' probably wont' work in this case.

Thanks.
I think I got that figured out.

var intersected = old_list.Intersect(new_list);

var resultsA = intersected.ToDictionary(x => x, x => old_list.IndexOf(x));
var resultsB = intersected.ToDictionary(x => x, x => new_list.IndexOf(x));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My code allows me to construct a new list based upon the common numbers. The following were output from my code.
Numbers: 2, 3, 4
Indices in old list: 1, 2, 3

Numbers: 2, 3, 4
Indices in new list: 2, 3, 4

Move value at old list[1] to new list[2]
Move value at old list[2] to new list[3]
Move value at old list[3] to new list[4]

Open in new window


What I didn't put down was to construct the new numbers "5" and "7" which can be obtained by comparing the "new_list" and the list in "resultsB". This gave me:
Add '5' to new list[0]
Add '7' to new list[1]

Open in new window


Thanks for your help.
Thanks Chris.