Files
30-seconds-of-code/snippets/find-parity-outliers.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

706 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Find parity outliers list,math beach-pineapple 2020-01-08T18:54:35+02:00 2020-11-02T19:27:53+02:00

Finds the items that are parity outliers in a given list.

  • Use collections.Counter with a list comprehension to count even and odd values in the list.
  • Use collections.Counter.most_common() to get the most common parity.
  • Use a list comprehension to find all elements that do not match the most common parity.
from collections import Counter

def find_parity_outliers(nums):
  return [
    x for x in nums
    if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]
  ]
find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]