Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 687 Bytes

counting-elements.MD

File metadata and controls

29 lines (24 loc) · 687 Bytes

Counting Elements (LeetCode)

https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3289/

class Solution {
public:
    int countElements(vector<int>& arr) {
        sort(arr.begin(), arr.end());
        
        int count = 0;
        
        int i = 0;
        while (i < arr.size()) {
            int next_i = i;
            for (; next_i < arr.size() && arr[i] == arr[next_i]; ++next_i);
            if (next_i == arr.size()) {
                break;
            }
            if (arr[next_i] - 1 == arr[i]) {
                count += (next_i - i);
            }
            i = next_i;
        }
        
        return count;
    }
};