Find The Parity Outlier in Java

The challenge

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]
Should return: 11 (the only odd number)

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

The solution in Java

Option 1:

import java.util.Arrays;

public class FindOutlier{
  static int find(int[] integers) {
    int[] array = Arrays.stream(integers).filter(i -> i % 2 == 0).toArray();  
    return array.length == 1 ? array[0] : Arrays.stream(integers).filter(i -> i % 2 != 0).findAny().getAsInt();
  }
}

Option 2:

public class FindOutlier{
  static int find(int[] integers) {
        int even = 0;
        int odd = 0;
        int cycle = 0;

        for(Integer value : integers) {
            if(value % 2 == 0) {
                cycle++;
                even = value;
            }else {
                odd = value;
            }
        }
        return (cycle > 1) ? odd : even;
    }
}

Option 3:

import static java.util.Arrays.stream;

public class FindOutlier {
  static int find(int[] integers) {
    final int p = stream(integers).limit(3).map(Math::abs).map(i -> i % 2).sum() / 2;
    return stream(integers).filter(i -> Math.abs(i) % 2 != p).findFirst().getAsInt();
  }
}

Test cases to validate our solution

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class OutlierTest{
 @Test
 public void testExample() {
     int[] exampleTest1 = {2,6,8,-10,3}; 
     int[] exampleTest2 = {206847684,1056521,7,17,1901,21104421,7,1,35521,1,7781}; 
     int[] exampleTest3 = {Integer.MAX_VALUE, 0, 1};
     assertEquals(3, FindOutlier.find(exampleTest1));
     assertEquals(206847684, FindOutlier.find(exampleTest2));
     assertEquals(0, FindOutlier.find(exampleTest3));
 }}
     
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments