w3resource logo


Python Exercises

Python Challenges: Find the single number in a list that doesn't occur twice

Python Challenges - 1: Exercise-12 with Solution

Write a Python program to find the single number in a list that doesn't occur twice.

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

Explanation :

Python: single number in a list that doesn't occur twice

Sample Solution :-

Python Code :

def single_number(arr):
    result = 0
    for i in arr:
        result ^= i
    return result

arr1 = [5, 3, 4, 3, 4]
arr2 = [3, 2, 5, 2, 1, 5, 3]

print(single_number(arr1))
print(single_number(arr2))

Output :

5                                                                       
1

Flowchart:

Python Flowchart: Find the single number in a list that doesn't occur twice

Python Code Editor:

def single_number(arr):
    result = 0
    for i in arr:
        result ^= i
    return result

arr1 = [5, 3, 4, 3, 4]
arr2 = [3, 2, 5, 2, 1, 5, 3]

print(single_number(arr1))
print(single_number(arr2))

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