diff --git a/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs index de91eac..7cddc61 100644 --- a/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs +++ b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs @@ -15,23 +15,20 @@ protected RansomNote() { } public static bool checkMagazineCompute(List magazine, List note) { - Dictionary dictionary = []; + Dictionary dictionary = []; foreach (string word in magazine) { - if (dictionary.TryGetValue(word, out int? value)) + if (!dictionary.TryAdd(word, 1)) { - dictionary[word] += 1; - } - else - { - dictionary[word] = 1; + int currentValue = dictionary[word]; + dictionary[word] = currentValue + 1; } } foreach (string word in note) { - if (dictionary.TryGetValue(word, out int? value) && value > 0) + if (dictionary.TryGetValue(word, out int value) && value > 0) { dictionary[word] -= 1; }