알고리즘 문제/CodeWar

Highest Rank Number in an Array

BEstyle 2022. 11. 19. 23:33

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