# プログラマの考えが・・身につく本より(Python)：最頻値を求める

More than 3 years have passed since last update.

C++での解答コードがすでに掲載されていますが、pythonで書き換えてみました。

test.cpp

```int mostFrequent;
int highestFrequency = 0;
int currentFrequency = 0;
for (int i = 0; i < ARRAY_SIZE; i++){
currentFrequency++;
if (i == ARRAY_SIZE - 1 || surveyData[i] != surveyData[i + 1]){
if (currentFrequency > highestFrequency){
highestFrequency = currentFrequency;
mostFrequent = surveyData[i];
}
currentFrequency = 0;
}
}
```

## 自分のpythonコード

test37.py

```#!/usr/bin/env python
#coding:utf-8

import math

elem = [4,7,3,8,9,7,3,9,9,3,3,10]

###修正させていただいています
#count = {Elem.count(v):v for v in set(Elem)}
#high = max(count)
#most = count[high]
elem.sort()
most = 0
high = 0
current = 0
#for i,v in enumerate(Elem):
for i in range(len(elem)):
current += 1
if i == len(elem) - 1 or elem[i] != elem[i+1]:
if current > high:
high = current
most = elem[i]
current = 0
print('配列=',elem, '\n' , '最頻値:',most,'|','回数:',high)

・・・(ターミナル実行)
>>> import test37

最頻値: 3 | 回数: 4
>>>

```