w3resource logo


Python Exercises

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.

Input : [ 4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4 ]
Output : 2

Explanation :

Python: Find the single number which occurs odd numbers and other numbers occur even number

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))

Output :

2 

Flowchart:

Python Flowchart: Find the single number which occurs odd numbers and other numbers occur even number

Python Code Editor:

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))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program