Find the most frequent element of a collection.

    :param elements: An iterable of elements
    :param to_hashable_f: (optional) if defined will be used to get
        hashable presentation for non-hashable elements. Otherwise json.dumps
        is used with sort_keys=True
    :return: element which is the most frequent in the collection and
        the number of its occurrences

def mostCommonElement(elements: Iterable[T], to_hashable_f: Callable=None):
    """
    Find the most frequent element of a collection.

    :param elements: An iterable of elements
    :param to_hashable_f: (optional) if defined will be used to get
        hashable presentation for non-hashable elements. Otherwise json.dumps
        is used with sort_keys=True
    :return: element which is the most frequent in the collection and
        the number of its occurrences
    """
    class _Hashable(collections.abc.Hashable):
        def __init__(self, orig):
            self.orig = orig

            if isinstance(orig, collections.Hashable):
                self.hashable = orig
            elif to_hashable_f is not None:
                self.hashable = to_hashable_f(orig)
            else:
                self.hashable = json.dumps(orig, sort_keys=True)

        def __eq__(self, other):
            return self.hashable == other.hashable

        def __hash__(self):
            return hash(self.hashable)

    _elements = (_Hashable(el) for el in elements)
    most_common, counter = Counter(_elements).most_common(n=1)[0]
    return most_common.orig, counter