Find most common value in an array

  • Thread starter Thread starter trollcast
  • Start date Start date
  • Tags Tags
    Array Value
Click For Summary

Discussion Overview

The discussion revolves around finding the mode of a list of numbers or the most common string in a list of names using Python. Participants explore the efficiency and correctness of a provided code snippet aimed at solving this problem.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • One participant presents a Python function intended to find the most common element in a list, asking for suggestions on improving its efficiency.
  • Another participant questions whether the code correctly identifies the most common element, suggesting it may only find elements that occur consecutively rather than throughout the entire list.
  • A later reply clarifies that the list is sorted before being passed to the function, which may affect the behavior of the code.
  • Concerns are raised about the initialization of the variable mostcommon, with suggestions that initializing it as an empty list would enhance code readability.
  • Participants discuss the implications of the code changes made by the original poster, indicating that the code appears acceptable after revisions.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the original code, with some agreeing that the sorted nature of the list makes the approach valid, while others remain uncertain about its functionality. The discussion does not reach a consensus on the optimal implementation.

Contextual Notes

There are unresolved questions regarding the efficiency of the proposed solution and the handling of edge cases, such as lists with multiple modes or empty lists.

trollcast
Gold Member
Messages
282
Reaction score
13

Homework Statement


Find the mode from a list of numbers or find the most common string in a list of names.

Homework Equations



The Attempt at a Solution



Code:
def most_common_element(lst):
    lst.sort()
    mostcommon = []
    mostcommonfreq = 0
    prev = 0
    count = 0

    for e in lst:
        if e != prev:
            count = 1
        if e == prev:
            count += 1

        if count == mostcommonfreq:
            mostcommon.append(e)

        if count > mostcommonfreq:
            mostcommon = [e]
            mostcommonfreq = count
        prev = e

    return mostcommon

Is there any way to make that more efficient?
 
Last edited:
Physics news on Phys.org
I'm not that familiar with python (assuming this is python), but it appears that this program finds the element that occurs the most times in a row, as opposed to the most times in the list.
 
rcgldr said:
I'm not that familiar with python (assuming this is python), but it appears that this program finds the element that occurs the most times in a row, as opposed to the most times in the list.

Sorry I should have copied more of the code.

I've sorted the list before I pass it to the most_common_element function.
 
Since the list is sorted your code looks ok. I noticed that you initialized mostcommon = 0 as opposed to mostcommon = [] (to create an empty list). It might make the code easier to follow using mostcommon = [], since the reader will know that mostcommon is to be used as a list.
 
rcgldr said:
Since the list is sorted your code looks ok. I noticed that you initialized mostcommon = 0 as opposed to mostcommon = [] (to create an empty list). Is this OK to do in Python?

Nope, I copied the source code from my program and then had to change it a bit so it made sense outside of the program.

I fixed it now
 
trollcast said:
I fixed it now
After your updates, the code looks OK now.
 
Last edited:

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K