'Python getting the key of the highest value in dictionary, without using inbuilt functions

How should we get the key of the highest value in python dictionary without using a inbuilt functions

{1: 1, 2: 1, 3: 1, 4: 3, 5: 2} **Expecting answer to be 4**

This can be easily done by

max_key = max(check, key=lambda k: check[k])

But wanted to try without builtin function(without max, lambda)

Any help is much appreciated

my full code

def array(num):
    check={}
    for i in range(len(num)):
        if num[i] in check:
            check[num[i]]+=1
        else:check[num[i]]=1

    max_key = max(check, key=lambda k: check[k])



array([1,2,3,4,5,4,5,4])


Solution 1:[1]

Function below perform simple loop thought dictionary and return biggest value of key without any builtins:

def get_max_val_key(data):
    max_value = None
    for key in data:
        if max_value is None or max_value < data[key]:
            max_value = data[key]
            max_key = key
    return max_key



data = {'a':11, 'b':12}
print(get_max_val_key(data))

Solution 2:[2]

Use this:

data= {1: 1, 2: 1, 3: 1, 4: 3, 5: 2}
maxVal= None
maxKey= -1
for k in data:
    if maxVal is None or maxVal< data[k]:
        maxVal= data[k]
        maxKey= k

Solution 3:[3]

score_card = {
    "James": 65,
    "William": 78,
    "Steven": 59,
    "Jason": 82,
    "Keith": 80
}


def highest_score(score_card):
  best = 0
  name = ""
  for player in score_card:
    score = score_card[player]
    if score > best:
      best= score
      name = player
   print(f"Highest Score is {best} by {name}")

highest_score(score_card)

Output: Highest Score is 82 by Jason

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andriy Ivaneyko
Solution 2
Solution 3