DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
easyaspython profile image
Dane Hillard • Edited

This goes through the list of numbers only a single time, stopping early if possible. Could probably be code golfed further 😛

def find_outlier(nums):
    odds = evens = 0
    for num in nums:
        is_even = num % 2 == 0
        is_odd = not is_even

        odds += 1 if is_odd else 0
        evens += 1 if is_even else 0

        # Two of one kind prove the other kind is the outlier
        if (odds > 1 and is_even) or (evens > 1 and is_odd):
            return num