Optimized count occurrences