Dictionary - Return highest total transaction amou

Run Settings
LanguageC#
Language Version
Run Command
using System; using System.Collections.Generic; using System.Linq; //Given a list of transaction records, where each record contains a user and an amount, //write a function that returns the top 2 users with the highest total transaction amounts. class MainClass { static void Main() { var transactions = new List<(string User, decimal Amount)> { ("Alice", 150), ("Bob", 200), ("Alice", 300), ("David", 50), ("Bob", 100), ("Charlie", 400) }; Dictionary<string, decimal> totalTrans = ProcessTotalTransactions(transactions); // Sort the dictionary by value in ascending order var sortedTransByValue = totalTrans.OrderByDescending(pair => pair.Value) .ToDictionary(pair => pair.Key, pair => pair.Value); int count = 0; foreach(var trans in sortedTransByValue){ if(count == 2){ break; } Console.WriteLine("[{0} : {1}]", trans.Key, trans.Value); count ++; } } public static Dictionary<string, decimal> ProcessTotalTransactions(List<(string User, decimal Amount)> inputList){ Dictionary<string, decimal> transactions = new Dictionary<string, decimal>(); foreach(var input in inputList){ if(transactions.ContainsKey(input.User)){ transactions[input.User] += input.Amount; } else{ transactions[input.User] = input.Amount; } } return transactions; } }
Editor Settings
Theme
Key bindings
Full width
Lines