The challenge
You are given an odd-length array of integers, in which all of them are the same, except for one single number.
Complete the method which accepts such an array, and returns that single different number.
The input array will always be valid! (odd-length >= 3)
Examples
[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3
The solution in Python
Option 1:
def stray(arr):
for x in arr:
if arr.count(x) == 1:
return x
Option 2:
def stray(arr):
return min(arr, key=arr.count)
Option 3:
def stray(arr):
return [x for x in set(arr) if arr.count(x) == 1][0]
Test cases to validate our solution
import codewars_test as test
from solution import stray
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(stray([1, 1, 1, 1, 1, 1, 2]), 2)
test.assert_equals(stray([2, 3, 2, 2, 2]), 3)
test.assert_equals(stray([3, 2, 2, 2, 2]), 3)