Thursday, March 2, 2023
HomeSoftware EngineeringLearn how to Discover the Stray Quantity in Python

# Learn how to Discover the Stray Quantity in Python

## The problem#

You might be given an odd-length array of integers, during which all of them are the identical, aside from one single quantity.

Full the strategy which accepts such an array, and returns that single totally different quantity.

The enter array will all the time be legitimate! (odd-length >= 3)

Examples

``````[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3
``````

Possibility 1:

``````def stray(arr):
for x in arr:
if arr.depend(x) == 1:
return x
``````

Possibility 2:

``````def stray(arr):
return min(arr, key=arr.depend)
``````

Possibility 3:

``````def stray(arr):
return [x for x in set(arr) if arr.count(x) == 1]
``````

## Take a look at circumstances to validate our answer#

``````import codewars_test as check

@check.describe("Fastened Exams")
def fixed_tests():
@check.it('Primary Take a look at Circumstances')
def basic_test_cases():
check.assert_equals(stray([1, 1, 1, 1, 1, 1, 2]), 2)
check.assert_equals(stray([2, 3, 2, 2, 2]), 3)
check.assert_equals(stray([3, 2, 2, 2, 2]), 3)
``````

RELATED ARTICLES