I am trying to use recursion to solve the OddOccurrencesInArray Problem in Codility, in which
- we are given an array with N elements, N is always odd
- all of the elements of the array except for one has a total even number of occurrences
- we need to write code that returns the one unpaired value
For example, if the array given is [9, 3, 9, 3, 7, 9, 9], the code must return 7, because that is the only element in the array which is unpaired.
My solution pseudocode/thought process was:
- sort the array
- if the first two elements are equal to each other, remove them and run the solution algorithm again recursively on the array minus the first two elements (after sorting) i.e. if the unpaired element is not found, we keep reducing the size of the array
- if the first two elements are NOT equal to each other, the first element of the array must be the unpaired item
My implementation was:
def solution(A):
# write your code in Python 3.6
if len(A) > 1:
A = sorted(A)
if A[0] != A[1]:
return A[0]
else:
solution(A[2:])
else:
return A[0]
I keep getting the error message
Invalid result type, int expected, <class 'NoneType'> found. RUNTIME ERROR (tested program terminated with exit code 1)
Can anyone help me figure out what this means and how I can correct it? Algorithmically, I think my solution is sound, and I don't understand why it isn't returning the integer values as I specified.
