[C#]Dictionary(辞書)で最小値からキーを取得するには?

C#

どうも、ちょげ(@chogetarou)です。

Dictionary(辞書)のValueの最小値からキー(key)を取得する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

Dictionary(辞書)のValueの最小値からキー(key)を取得する方法は、3つあります。

Aggregate()

1つ目は、Aggregate()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

DictionaryからAggregate()を呼び出します。

Aggregate()の引数に、第1引数と第2引数のValueプロパティで小さい方を返すラムダ式を指定します。

そして、Aggregate()の結果のKeyプロパティにアクセスします。

T minKey = myDict.Aggregate((x, y) => x.Value < y.Value ? x : y).Key;

上記のKeyプロパティは、Aggregate()を呼び出したDictionary(辞書)のValueの最小値のキーを返します。

使用例

using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
    public static void Main()
    {
        Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "ten", -10 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    string minKey = numbers.Aggregate((x, y) => x.Value < y.Value ? x : y).Key;
	    
	    Console.WriteLine(minKey); //ten
	}
}

foreachループ

2つ目は、foreachループを使う方法です。

まず、KeyValuePairの変数を用意します。

KeyValuePair<TKey, TValue> minItem = new KeyValuePair<TKey, TValue>();

foreachでDictionaryをループします。

foreachのループ処理で、用意した変数よりループ変数の方がValueプロパティが小さい場合に、ループ変数を用意した変数に代入します。

foreach(var item in myDict)
{
     if (item.Value < minItem.Value)
     {
          minItem = item;
     }
}

あとは、用意した変数のKeyプロパティを取得します。

T minKey = minItem.Key;

上記のKeyプロパティは、DictionaryのValueの最小値のkeyを返します。

使用例

using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
        Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "ten", -10 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    KeyValuePair<string, int> minItem = new KeyValuePair<string, int>();
	    
	    foreach(var item in numbers)
        {
            if (item.Value < minItem.Value)
            {
                minItem = item;
            }
        }   
	    
	    string minKey = minItem.Key;
	    
	    Console.WriteLine(minKey); //ten
	}
}

OrderBy()

3つ目は、OrderBy()を使う方法です。

まず、System.Linqを導入します。

using System.Linq;

次に、DictionaryからOrderBy()を呼び出します。

OrderBy()の引数に、引数のValueプロパティを返すラムダ式を指定します。

そして、OrderBy()のKeyプロパティにアクセスします。

T minKey = myDict.OrderBy(x => x.Value).First().Key;

上記のKeyプロパティは、DictionaryのValueの最小値のkeyを返します。

使用例

using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
    public static void Main()
    {
        Dictionary<string, int> numbers = new Dictionary<string, int>()
	    {
	        { "one", 1 },
	        { "two", 2 },
	        { "three", 3 },
	        { "ten", -10 },
	        { "four", 4 },
	        { "five", 5 },
	    };
	    
	    string minKey = numbers.OrderBy(x => x.Value).First().Key;
	    
	    Console.WriteLine(minKey); //ten
	}
}

まとめ

Dictionary(辞書)のValueの最小値からキー(Key)を取得する方法は、次の3つです。

  • Aggregate()を使う方法
    T minKey = myDict.Aggregate((x, y) => x.Value < y.Value ? x : y).Key;
  • foreachループを使う方法
  • OrderBy()を使う方法
    T minKey = myDict.OrderBy(x => x.Value).First().Key;

コメント

タイトルとURLをコピーしました