Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int CountElements(int[] arr)
- {
- var dict = new Dictionary<int, int>();
- var sum = 0;
- foreach (var num in arr)
- {
- if (dict.TryGetValue(num, out var count))
- {
- dict[num] = count + 1;
- }
- else
- {
- dict.Add(num, 1);
- }
- }
- foreach (var (key, value) in dict)
- {
- if (dict.TryGetValue(key + 1, out var _value))
- {
- sum += value;
- }
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement