Linq Group by



Output Format

Return the total number of matching pairs of socks that Alex can sell.

Sample Input

9
10 20 20 10 10 30 50 10 20

Sample Output

3
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

    // Complete the sockMerchant function below.
    static int sockMerchant(int n, int[] ar) {

            var x = from a in ar 
                    group a by a into g
                    select new { key = g.Key, countValue = g.Count()};

            int total = 0;
            foreach (var y in x){
                
               total += y.countValue/2;

            }

            return total;
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());

        int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp))
        ;
        int result = sockMerchant(n, ar);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}










================================================================
之前成日都係到要時做sorting 找最大, 用foreach 就好易time out , 所以要善用一下Linq.

Sample Input 0

4
3 2 1 3

Sample Output 0

2


static int birthdayCakeCandles(int[] ar) {

            var x = from a in ar 
                    group a by a into g
                    select new { key = g.Key, countValue = g.Count()};

             var maxCount = x.OrderByDescending(y=>y.countValue).FirstOrDefault();

             return maxCount.countValue;   
    }

我都覺得 下面個sameple 係幾好 class Person { public string Name { get; set; } public string City { get; set; } public int Age { get; set; } } ... List<Person> people = new List<Person>{ new Person{Name="Peter", City="KHH", Age=40}, new Person{Name="Eden", City="TPE", Age=35}, new Person{Name="Scott", City="KHH", Age=27}, new Person{Name="Tim", City="TPE", Age=18} };


四組方法的應用

分別用不同的方法取得每個城市的人數、最大及最小年齡,得到的結果如下:

City: KHH

    Count: 2
    Min: 27
    Max: 40


City: TPE

    Count: 2
    Min: 18
    Max: 35

var result = personList.GroupBy(x => x.City, x=> x.Age,  (city, ages) => new
{
    City = city,
    Count = ages.Count(),
    Min = ages.Min(age => age),
    Max = ages.Max(age => age)
});

foreach (var cityInfo in result)
{
    Console.WriteLine($"    City: {cityInfo.City}");
    Console.WriteLine($"        Count: {cityInfo.Count}");
    Console.WriteLine($"        Min: {cityInfo.Min}");
    Console.WriteLine($"        Max: {cityInfo.Max}");
    Console.WriteLine();
}

最後一組方法則可以簡化resultSelector的處理,使其可以專注於它的對象資料(age)就好。

這個例子利用了四組方法各個不同的特性,將相同的資料作輸出,雖然越後面的方法,在執行完後需要做的處理越少,但是每個方法都有適用於它的情境,工程師可以就需要查詢的資料做最適當的選擇。




留言

這個網誌中的熱門文章

香港袐密行動

要老是忘記, 我更記不起