forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1426-CountingElements.cs
More file actions
31 lines (26 loc) · 864 Bytes
/
1426-CountingElements.cs
File metadata and controls
31 lines (26 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//-----------------------------------------------------------------------------
// Runtime: 100ms
// Memory Usage: 24.2 MB
// Link: https://leetcode.com/submissions/detail/321449442/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1426_CountingElements
{
public int CountElements(int[] arr)
{
var mapping = new SortedDictionary<int, int>();
foreach (var num in arr)
{
if (!mapping.ContainsKey(num)) mapping.Add(num, 0);
mapping[num]++;
}
var result = 0;
foreach (var num in mapping.Keys)
if (mapping.ContainsKey(num + 1))
result += mapping[num];
return result;
}
}
}