-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFindUniqueNumber.java
42 lines (35 loc) · 1.1 KB
/
FindUniqueNumber.java
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
35
36
37
38
39
40
41
42
package main.java.kyu6;
/**
* 6 kyu - Find the unique number
*
* https://www.codewars.com/kata/585d7d5adb20cf33cb000235/
*
* Details:
*
* There is an array with some numbers. All numbers are equal except for one. Try to find it!
*
* It’s guaranteed that array contains at least 3 numbers.
*
* The tests contain some very huge arrays, so think about performance.
*/
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
public class FindUniqueNumber {
private double precision = 0.0000000000001;
public static double findUniq(double arr[]) {
return Arrays.stream(arr)
.distinct()
.filter(a -> Arrays.stream(arr)
.filter(b -> a == b).count() == 1)
.reduce((a, b) -> {
throw new IllegalStateException();
})
.getAsDouble();
}
@Test
public void sampleTestCases() {
assertEquals(1.0, findUniq(new double[]{0, 1, 0}), precision);
assertEquals(2.0, findUniq(new double[]{1, 1, 1, 2, 1, 1}), precision);
}
}