Skip to content
Advertisement

Getting the max and min from a dictionary without using max() and min()

I have a dictionary with two values (id and amount):

JavaScript

and I want to find the id with the max and min amount from the dictionary without using the max and min functions.

So desired output is:

JavaScript

Advertisement

Answer

You could just calculate it with a for loop:

JavaScript

If your values are lists, you need to chose what index to use to compare the list with. In the code below I hardcoded zero. If you want to go through the list and find the max value there, that would just be another nested loop.

JavaScript

To get the min you would use this exact same process but switch the operator to less than.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement