From 706e4e5b539546be58df9396bfa03015a5dfa66d Mon Sep 17 00:00:00 2001 From: kamalelhaddad Date: Sat, 23 Oct 2021 14:17:55 +0200 Subject: [PATCH] Update Solution.cs --- .../Implementation/Sock Merchant/Solution.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Algorithms/Implementation/Sock Merchant/Solution.cs b/Algorithms/Implementation/Sock Merchant/Solution.cs index 1ba4e06..9a395e0 100644 --- a/Algorithms/Implementation/Sock Merchant/Solution.cs +++ b/Algorithms/Implementation/Sock Merchant/Solution.cs @@ -26,20 +26,13 @@ class Solution { static int sockMerchant(int[] socksPile) { - var pairsFound = 0; - var sockColorHash = new Dictionary(); - - foreach (var sock in socksPile) - { - if (sockColorHash.ContainsKey(sock)) - { - pairsFound++; - sockColorHash.Remove(sock); + var pairsFoundSum = 0; + var grps = socksPile.GroupBy(i => i); + foreach (var grp in grps) { + pairsFoundSum += grp.Count() / 2; } - else - sockColorHash.Add(sock, 1); - } - return pairsFound; + + return pairsFoundSum; } static void Main(String[] args) {