Find the unique number
There is an array with some numbers. All numbers are equal except for one. Try to find it!
Kata.findUniq(new double[]{ 1, 1, 1, 2, 1, 1 }); // => 2
Kata.findUniq(new double[]{ 0, 0, 0.55, 0, 0 }); // => 0.55
It’s guaranteed that array contains at least 3 numbers.
The tests contain some very huge arrays, so think about
performance.
This is the first kata in series:
Find the unique number (this kata)
Find the unique string
Find The Unique
Code(Java Solution)
static double findUniq(double arr[])
{
Arrays.sort(arr);
if(arr[0]==arr[1])
return arr[arr.length-1];
else
return arr[0];
}
Code(Python Solution)
def
findUniq(arr):
elements_count = {}
for element
in arr:
if element
in elements_count:
elements_count[element]
+= 1
else:
elements_count[element]
= 1
for key,
value in
elements_count.items():
if(value==1):
0 Comments
Post a Comment