-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindTheParityOutlier.java
More file actions
34 lines (30 loc) · 907 Bytes
/
FindTheParityOutlier.java
File metadata and controls
34 lines (30 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
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)
*/
public class FindOutlier{
static int find(int[] integers){
int firstOdd = 0;
int firstEven = 0;
int countEven = 0;
for (int i = 0; i < integers.length; i++) {
if (integers[i] % 2 == 0) {
firstEven = integers[i];
countEven++;
}
else {
firstOdd = integers[i];
}
}
if (countEven > 1)
return firstOdd;
else
return firstEven;
}
}