I have a list. I would like to take the last value from each run of similar elements.
What I mean? Let me give you a simple example. Given a list of words
['golf', 'hip', 'hop', 'hotel', 'grass', 'world', 'wee']
And the similarity function "starting with the same letter", the function will return a shorter list
['golf', 'hotel', 'grass', 'wee']
Why? The original list has 1st order of G words, 3 of H words, 1st of G words and 2 round W-words. The function returns the last word from each run.
How can i do this?
C # hypothetical syntax (I actually work with client objects, but I want to share what you could run and check yourself)
> var words = new List<string>{"golf", "hip", "hop", "hotel", "grass", "world", "wee"}; > words.LastDistinct(x => x[0]) ["golf", "hotel", "grass", "wee"]
Edit: I tried .GroupBy(x => x[0]).Select(g => g.Last()) , but it gives ['grass', 'hotel', 'wee'], which is not what I want. Read the example carefully.
Change Another example.
['apples', 'army', 'black', 'beer', 'bastion', 'cat', 'cart', 'capable', 'art', 'bark']
There are 5 starts (start A, start B, start C, new start A, new start B). Last word from each run:
['army', 'bastion', 'cart', 'art', 'bark']
It is important to understand that each run is independent. Do not mix start A at the beginning with start A near the end.