python Find the only element appearing once in a sequence using the more efficient way than mine [closed]

You could use a Counter to count the elements in the list. Since you know there’s exactly one element that appears once, it will be the least common element, so:

def singleNumber(nums):
    return Counter(nums).most_common()[-1][0]

Leave a Comment