Python Challenges: Find the single number which occurs odd numbers and other numbers occur even number
Python Challenges - 1: Exercise-32 with Solution
Write a Python program to find the single number which occurs odd numbers and other numbers occur even number.
Explanation:
Sample Solution:
Python Code:
def odd_occurrence(arr):
# Initialize result
result = 0
# Traverse the array
for element in arr:
# XOR
result = result ^ element
return result
# Test data
num1 = [ 4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4 ]
print(odd_occurrence(num1))
Sample Output:
2
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to add two binary numbers.
Next: Write a Python program to compute the sum of all the multiples of 3 or 5 below 500.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/challenges/1/python-challenges-1-exercise-32.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics