Search code examples
pythonfrequency

Most-frequency number


Yes there are lots of topic about this. but my question is different. I wrote my code and it is;

A = [1,2,3,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8,8,7,7]

def countFreq(A):
    n=len(A)
    count=[0]*n                     # Create a new list initialized with '0'
    for i in range(n):
        count[A[i]]+= 1              # increase occurrence for value A[i]
    result = []
    for x in count:
        if x:
            result.append(x)
    return result
print(countFreq(A))

output is;

[1,1,1,3,1,5,3,6]

that means, how many numbers there are, that function it counted. there is one "1" and there are five "6" and six "8" etc.

but I want to just seeing most frequency number. as that code, just seeing 8. how can I do? Please show a way with my code. not different code. I don't want to built-in function or any ready code. please see a way just in my code.

edit: Actually, I want to see from my code's output;

A = [1,2,3,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8,8,7,7]
Most-frequency number is : 8

or

A = [1,2,3,3,3]
Most frequency number is : 3

or

A = [1,1,1,2,3,4]
most frequency number is : 1

I want this..

edit: In my question, teacher give us a hint, this is; "Hint: You may consider recording the number of occurrences of each digit in a new list, whose indices will be mapped to the input digit values."

Please anyone that see it, show me a way with my code...


Solution

  • Change your print() statement to print(countFreq(A).index(max(countFreq(A))) + 1).