Resolved how do you compare ordered lists and find the difference int values in the list?

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
97
Programming Experience
1-3
Good day, everyone!

I have two lists (before and after) with me that contain integer values. The only difference between the two lists is that if one of the values in the before list is 1, then one of the values in the after list will be 1 + 1 = 2.

Which I need to compare, but I'm having trouble doing it. Can someone assist me?

Here's the code and the problem I'm having.

Before List: 31,5,2,3,2,1

After List: 31,5,2,3,2,2

C#:
yield return !Enumerable.SequenceEqual(beforeClickNumberOfClicks, afterClickNumberOfClicks);

var beforeClickNumberOfClickForTag = beforeClickNumberOfClicks.Except(afterClickNumberOfClicks);
var afterClickNumberOfClickForTag = afterClickNumberOfClicks.Except(beforeClickNumberOfClicks);

yield return beforeClickNumberOfClickForTag.Count().Equals(1);
yield return afterClickNumberOfClickForTag.Count().Equals(1);

yield return (beforeClickNumberOfClickForTag.First() + 1).Equals(afterClickNumberOfClickForTag.First());
 
You said that the lists are ordered, but I see 3 appearing after a 2 when the numbers look to be in descending order.
 
C#:
before.Zip(after)
      .Select((pair, index) => (pair, index))
      .Where(x => x.pair.First != x.pair.Second)
 
Solution
You said that the lists are ordered, but I see 3 appearing after a 2 when the numbers look to be in descending order.
Yes - it won't be as desc or asc but only difference would be both the list are having same value at same index except last index - so I want to find that index values only
 
In .Net framework projects you also have to provide a resultSelector, change before.Zip(after) to before.Zip(after, (First, Second) => (First, Second))
 
What about good old for loop?
C#:
Debug.Assert(before.Count == after.Count);
for(int i = 0; i < before.Count; i++)
{
    if (before[i] != after[i])
    {
        // do something about the mismatched pair at index i

        break;
    }
}
 
Last edited:
Back
Top Bottom