Find The Parity Outlier

jenny
Member
Lid geworden: 2025-08-24 15:11:06
2024-06-03 16:46:27

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.

Examples
[2, 4, 0, 100, 4, 11, 2602, 36] --> 11 (the only odd number)

[160, 3, 1719, 19, 11, 13, -21] --> 160 (the only even number)

Image preview

blessedtechie
Beheerder
Lid geworden:
2024-06-03 18:47:12

Image preview

This problem brings back memories of a classic computer science homework: identifying the outlier in a list of numbers. Here's a clever solution that leverages the parity (even or odd) of the first few elements.

  • 1.

    **Parity Check:** We start by examining the parity of the first three numbers in the list. Even numbers result in a remainder of 0 when divided by 2, while odds yield a remainder of 1.

  • 2.

    **Majority Rules:** By storing these remainders in a list, we can determine the prevailing parity (even or odd) in the majority.

  • 3.

    **Find the Outlier:** Finally, we iterate through the entire list. The first number whose parity doesn't match the majority is our outlier!

  • This approach offers an efficient way to pinpoint the outlier, making it a valuable technique to have in your problem-solving toolbox.

    jenny
    Member
    Lid geworden: 2025-08-24 15:11:06
    2024-07-02 22:18:31

    Outlier in an array is a very good interview question too, it will get you sweating a bit. No, did I say good? I meant terrible!

    jenny
    Member
    Lid geworden: 2025-08-24 15:11:06
    2024-07-25 10:37:28
    [[9,31],[29]]
    Facebook X (Twitter) Instagram LinkedIn Telegram WhatsApp