0

I've a list that contains 4 sized arrays:

enter image description here

These arrays have 4 elements. I want to use another list that contains these arrays' first element's count. In addition, if their first elements are same, they should be summation. For example:

list[0] = {1,2,3,4}
list[1] = {1,1,5,3}
list[2] = {1,2,5,8}
list[3] = {2,2,3,3}
list[4] = {3,5,5,6}
list[5] = {4,4,4,4}
list[6] = {4,5,5,6}

So, anotherList should be:

anotherList = {3, 1, 1, 2}

How can I do this?

EDIT: Expected result is:

enter image description here

10
  • 4
    I don't follow your example at all. Why 3, 1, 1, 2? What does that have to do with summation? What do you mean by "first elements count"? I'm entirely confused. Commented May 22, 2014 at 14:05
  • What stoped you from doing this? (What have you tried so far) Commented May 22, 2014 at 14:05
  • @JonSkeet if this was sql it would be equivalent to the sql select count(column_1) from list group by column_1 Commented May 22, 2014 at 14:06
  • @ScottChamberlain: And for some reason it's 1-based rather than 0-based? Okay. Still no idea where "summation" comes in - I'm glad you managed to follow the question, as it still seems bizarrely explained to me. Commented May 22, 2014 at 14:10
  • @JonSkeet It's 1 based because sql <3's its 1 based indexes. Commented May 22, 2014 at 14:14

1 Answer 1

6
anotherList = list.Select(a => a[0]) // project each array to its first item
                  .GroupBy(x => x)   // group first items by their value
                  .Select(g => g.Count())  // select count of same items
                  .ToList(); 

Output:

[ 3, 1, 1, 2 ]

NOTE: GroupBy internally uses Lookup which returns groups in same order as the are added, so it seems to be what you want.

UPDATE: Approach which does not depend on internal implementation of GroupBy

anotherList = list.Select((a,i) => new { Item = a[0], Index = i })
                  .GroupBy(x => x.Item)
                  .OrderBy(g => g.Min(x => x.Index))
                  .Select(g => g.Count())
                  .ToList();
Sign up to request clarification or add additional context in comments.

7 Comments

lol, and this is why I like linq, you took the words right out of my mouth.
One note if the behavior of Lookup, the output order is not "well defined" so at any later time they could choose to change how ordering is handled.
@ScottChamberlain yes, completely agree with you! But currently test passes and I believe its simplest solution. Anyway, added another, implementation independent solution:)
@team16sah I recommend LINQ Fundamentals course by pluralsight, and Re-implementing LINQ series
@ScottChamberlain: The order of Enumerable.GroupBy is well-defined. From the Enumerable.GroupBy documentation: "The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping are yielded in the order they appear in source."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.