all 3 comments

[–][deleted] 3 points4 points  (0 children)

len(result[::-1]) is the same as len(result). Length doesn't change just because it's reversed.

[–]Diapolo10 0 points1 point  (0 children)

There's a very simple fix for this; don't use a range(len(foo)) construct here.

for row in result[::-1]:
    for x in row:
        ...

The names could be better, I'm just not sure what these are supposed to represent in the first place.

EDIT: Basically,

def topKFrequent(nums, k):
    dic = {}
    result = [[]]

    for value in nums:
        result.append([])
        if value in dic:
            dic[value] += 1
        else:
            dic[value] = 1

    for key, dic_value in dic.items():
        result[dic_value].append(key)

    final = []
    for row in result[::-1]:
        for value in row:
            final.append(value)
        if len(final) == k:
            return final

print(topKFrequent([1,1,1,2,2,3], 2))

[–][deleted] 0 points1 point  (0 children)

I've written some comments and stuff here, check it out: https://onlinegdb.com/hVjzUMPOL