DESCRIPTION:
Complete the method which returns the number which is most frequent in the given input array. If there is a tie for most frequent number, return the largest number among them.
Note: no empty arrays will be given.
Examples
[12, 10, 8, 12, 7, 6, 4, 10, 12] --> 12
[12, 10, 8, 12, 7, 6, 4, 10, 12, 10] --> 12
[12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10] --> 3
def highest_rank(arr):
adict={}
for i in arr:
if i not in adict:
adict[i]=1
else:
adict[i]=adict[i]+1
max=0
ans=0
for i in adict:
if adict[i]>max:
print(i,adict[i])
max=adict[i]
ans=i
elif adict[i]==max:
if i>ans:
ans=i
return ans
'알고리즘 문제 > CodeWar' 카테고리의 다른 글
Run-length encoding (1) | 2022.11.20 |
---|---|
Descending Order (0) | 2022.11.20 |
Range Extraction (0) | 2022.11.18 |
Sort Arrays(Ignoring case) (0) | 2022.11.18 |
Sum two arrays (0) | 2022.11.18 |