Skip to content

Commit 91c436b

Browse files
authored
Merge pull request #82 from sir-gon/develop
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: H…
2 parents ad5d757 + e7fca54 commit 91c436b

File tree

1 file changed

+5
-8
lines changed
  • algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps

1 file changed

+5
-8
lines changed

algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@ protected RansomNote() { }
1515

1616
public static bool checkMagazineCompute(List<string> magazine, List<string> note)
1717
{
18-
Dictionary<string, int?> dictionary = [];
18+
Dictionary<string, int> dictionary = [];
1919

2020
foreach (string word in magazine)
2121
{
22-
if (dictionary.TryGetValue(word, out int? value))
22+
if (!dictionary.TryAdd(word, 1))
2323
{
24-
dictionary[word] += 1;
25-
}
26-
else
27-
{
28-
dictionary[word] = 1;
24+
int currentValue = dictionary[word];
25+
dictionary[word] = currentValue + 1;
2926
}
3027
}
3128

3229
foreach (string word in note)
3330
{
34-
if (dictionary.TryGetValue(word, out int? value) && value > 0)
31+
if (dictionary.TryGetValue(word, out int value) && value > 0)
3532
{
3633
dictionary[word] -= 1;
3734
}

0 commit comments

Comments
 (0)