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
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
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 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?
can you give any example?
ASKER
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.
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.
ASKER
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));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My code allows me to construct a new list based upon the common numbers. The following were output from my code.
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:
Thanks for your help.
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]
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]
Thanks for your help.
ASKER
Thanks Chris.
Open in new window
resultOpen in new window
https://dotnetfiddle.net/Bg0WmA