Count Vowels

easy
By FrontendAtlas Editorial · Updated Oct 10, 2026
Implement countVowels(str) that returns how many vowels appear in a string. Normalize case, decide whether to include 'y', and handle empty input safely. Use a Set for O(1) membership checks and a single O(n) pass. Concepts: strings, iteration, counting, sets.

Arguments

  • str: string — The input string to analyze.

Returns

number — The number of vowels found in the string.

Solution idea

Scan the string once, normalize case, and count only characters that belong to a vowel set. This keeps the solution O(n), handles empty input gracefully, and avoids tricky regex edge cases. It’s a classic example of membership checks with a Set. Key concepts: strings, iteration, counting.

Examples
// Basic examples
countVowels('hello') // => 2

countVowels('rhythm') // => 0

countVowels('AEIOU') // => 5

// Mixed case and spaces
countVowels('A quick brown fox') // => 5