how can I remove the duplicate value next to each other using linq

Zackelberry 1 Reputation point
2021-09-30T15:49:18.437+00:00

I have a list and I want to remove the duplicate value next to each other

var idList = new List<int>(){100,100,200,200,300,300,100,100,200,200,300,300}

and this is my expected result

[100,200,300,100,200,300]

How can i achieve this in linq

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-09-30T17:00:13.707+00:00

    Try one of solutions:

    var result = idList
            .Take( 1 )
            .Concat( idList
                        .Skip( 1 )
                        .Zip( idList, ( a, b ) => new { a, b } )
                        .Where( p => p.a != p.b )
                        .Select( p => p.a ) )
            .ToList( );
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.