编写一个函数,接收一个字符串作为参数,并返回该字符串中出现次数最多的字符
时间: 2023-05-31 17:03:26 浏览: 958
以下是Python的实现:
```python
def most_frequent_char(s):
freq_dict = {}
for c in s:
freq_dict[c] = freq_dict.get(c, 0) + 1
most_freq_char = None
max_freq = 0
for c, freq in freq_dict.items():
if freq > max_freq:
most_freq_char = c
max_freq = freq
return most_freq_char
```
该函数先创建一个字典`freq_dict`,用于统计每个字符在字符串`s`中出现的次数。然后遍历字典,找到出现次数最多的字符,并返回该字符。如果有多个字符出现次数相同且最多,则返回其中任意一个。
阅读全文