Find Most Frequent Character

Find character that appears most frequently.

Logic BuildingAdvanced
Logic Building
# Take string input
s = input("Enter a string: ")

# Count frequency
freq = {}
for char in s:
    if char != ' ':  # Ignore spaces
        freq[char] = freq.get(char, 0) + 1

# Find most frequent
if freq:
    most_frequent = max(freq, key=freq.get)
    print(f"Most frequent: {most_frequent} (appears {freq[most_frequent]} times)")
else:
    print("No characters found")

Output

Enter a string: hello
Most frequent: l (appears 2 times)

Count frequency and find maximum.

Key Concepts:

  • Count frequency of each character
  • Use max() with key=freq.get
  • Returns most frequent character