import itertools
import operator


def most_common(a_list: list):
    """Get most common element in a list

    Args:
        a_list (list): Any arbitrary list

    Returns:
        any: pick the highest-count/earliest item
    """
    # get an iterable of (item, iterable) pairs
    sorted_list = sorted((x, i) for i, x in enumerate(a_list))
    groups = itertools.groupby(sorted_list, key=operator.itemgetter(0))

    def _auxfun(grp):
        """Auxiliary function to get "quality" for an item

        This function should be used in tandem with max()

        Args:
            grp (iterable): an object to returned by max() if the provided
            iterable passed to max() is empty.
        """
        item, iterable = grp
        count = 0
        min_index = len(a_list)
        for _, where in iterable:
            count += 1
            min_index = min(min_index, where)

        return count, -min_index

    return max(groups, key=_auxfun)[0]