ものともしれない日々

参考にできるかかもしれない備忘録

配列の重複を数える処理メモ

便利だけど書き方すぐ忘れるよ・・・

using System;
using System.Collections.Generic;
using System.Linq;

public class Hello{
    public static void Main(){

        List<string> dataList = new List<string>() { "HND", "NRT", "KIX", "NGO", "NGO", "NGO", "NGO", "NGO" };
        var duplicationList = dataList
            .GroupBy(i => i)
            .Where(g => g.Count() > 1)
            .Select(g => new { word = g.Key, duplicationCount = g.Count() })
            .ToList();
        foreach(var item in duplicationList) {
            Console.WriteLine("{0}:{1}個", item.word, item.duplicationCount);
        }

    }
}