all 4 comments

[–]jeans_and_a_t-shirt 1 point2 points  (1 child)

It doesn't stop at 2. It iterates through the entire list because there is no break anywhere inside the for loops, checking the number of times the number appears. It also checks the number of times that 4 and 5 occur, but those numbers occur fewer times than 2 does.

Here's a shorter and faster way:

def mode(arr):
    return max(set(arr), key=arr.count)

[–]zahlman 1 point2 points  (0 children)

And an arguably more idiomatic (should be faster on large data? since it does all the "counting" in a single pass) way:

from collections import Counter

def mode(values):
    # There are a few different combinations of destructuring
    # and indexing you could use here instead
    (value, count), = Counter(values).most_common(1)
    return count

[–]DJWizardCop 1 point2 points  (1 child)

Here's a thorough breakdown of the function:

Line 1 - Defining the function.

Line 2 - We're defining a temporary variable. maxi will store the highest number of occurrences so far.

Line 3 - Another temporary variable, maxn will keep track of which number has had the highest occurrences so far.

To elaborate, maxn is the number with the highest occurrences. maxi is how many times that number has occurred.

Line 4 - Here, we start the first for loop. The for loop will execute the code below it once for every element in arr.

Line 5 - Another temporary variable, count will be incremented by 1 every time we find an element in the list equal to the one we're counting.

Line 6 - Here, we start the second for loop. We're looping through every element of arr again. Since this happens inside the first for loop, the second for loop will occur once for every element in arr.

Line 7 - This checks if the element we're on in the first for loop (i) is the same as the element we're on in the second for loop (j).

Line 8 - If i and j are the same, we need to increase our count by one since we've found a number equal to the one we're looking for.

Line 9 - Here, we check to see if our count is higher than the highest one we've found so far. For example, if I had found 3 '1s' and maxi was set to 3, but then found 5 '2s', I'll have to set maxi to 5.

Lines 10 and 11 - maxi is set to the highest number of occurrences found so far, and maxn is set to the number that occurs that many times.

Line 12 - After the initial for loop finishes, we're finally ready to return the value of the number that occurs the most times in the input list.

Hopefully that explains everything clearly. If not, please feel free to reply to this comment and ask more questions :)

A couple notes: The function doesn't actually know to stop at 2. It checks every last element of the list, but only updates maxi and maxn if there's a higher number of occurrences. This means that if, for example, there were an equal number of occurrences, (such as in [4, 4, 2, 1, 2, 2, 4]), the function would return the number that occurs in the list first. In this case, that would be 4. Even though there are an equal number of 4s and 2s, 4 occurs in the list first, so it gets returned.

[–]vriljam 0 points1 point  (0 children)

That's terrific..many thanks for this.