Find Minimum (Min) and Maximum (Max) value from Generic List of KeyValuePair using C# and VB.Net

Last Reply 11 months ago By pandeyism

Posted 11 months ago

I have a multidimensional array with basically a key / value pair. I would like to sum all the values according to the key then pick the min and max values from this.

I tried making a List<keyvaluepair> and a dictionary to do this I got very close but it wouldn’t let me sort through a list of doubles. My lists and arrays are created correctly without issues. I would like to display the min/max of value and also display the key in the same line.

        public static void PrintTable()
        {
            string text;
            var totals = new List<KeyValuePair<string, double>>();
            for(int i = 0; i < topics.Length; i++)
            {
                Console.Write(topics[i]  + " ");
                double num = 0;
                for(int j = 0; j < 10; j++)
                {
                    Console.Write(responses[i, j] + " ");
                    num += responses[i, j] * (j + 1);
                    totals.Add(new KeyValuePair<string, double>( topics[i], num));
                }
                average = num / userCounter;
                Console.Write("\t" + average.ToString("##.00"));
            }
            // Display highest points and lowest points.
            var addedSums = totals.GroupBy(k => k.Key).ToDictionary(a => a.Key, b => b.Sum(c => c.Value));
            foreach(var pair in addedSums.Values.OrderByDescending())
            {
                Console.WriteLine("Highest points: " + pair);
            }
        }

 

Posted 11 months ago Modified on 11 months ago

Hey bigbear,

Please refer below sample.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    var totals = new List<KeyValuePair<string, double>>();
    totals.Add(new KeyValuePair<string, double>("mudassar", 1.5));
    totals.Add(new KeyValuePair<string, double>("john", 5.8));
    totals.Add(new KeyValuePair<string, double>("robert", 33.1));
    totals.Add(new KeyValuePair<string, double>("test", 0.0));
    double minValue = 0;
    double maxValue = 0;
    string maxKey = "";
    string minKey = "";
    foreach (KeyValuePair<string, double> item1 in totals)
    {
        if (maxValue < item1.Value)
        {
            maxKey = item1.Key;
            maxValue = item1.Value;
        }
    }

    minValue = maxValue;
    foreach (KeyValuePair<string, double> item in totals)
    {
        if (item.Value != 0)
        {
            if (minValue > item.Value)
            {

                minKey = item.Key;
                minValue = item.Value;
            }
        }
    }
    Response.Write(minKey + " Minimum value is :  " + minValue + " <br />" + maxKey + " Maximum Value is : " + maxValue);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim totals = New List(Of KeyValuePair(Of String, Double))()
        totals.Add(New KeyValuePair(Of String, Double)("mudassar", 1.5))
        totals.Add(New KeyValuePair(Of String, Double)("john", 5.8))
        totals.Add(New KeyValuePair(Of String, Double)("robert", 33.1))
        totals.Add(New KeyValuePair(Of String, Double)("test", 0.0))
        Dim minValue As Double = 0
        Dim maxValue As Double = 0
        Dim maxKey As String = ""
        Dim minKey As String = ""

        For Each item1 As KeyValuePair(Of String, Double) In totals

            If maxValue < item1.Value Then
                maxKey = item1.Key
                maxValue = item1.Value
            End If
        Next

        minValue = maxValue

        For Each item As KeyValuePair(Of String, Double) In totals
            If item.Value <> 0 Then
                If minValue > item.Value Then
                    minKey = item.Key
                    minValue = item.Value
                End If
            End If
        Next
        Response.Write(minKey & " Minimum value is :  " & minValue & " <br />" & maxKey & " Maximum Value is : " & maxValue)
    End Sub

Output

mudassar Minimum value is : 1.5

robert Maximum Value is : 33.1