# 查找奇数次出现的数字的Python程序

## 本文概述

``````Input : arr = {1, 2, 3, 2, 3, 1, 3}
Output : 3

Input : arr = {5, 7, 2, 7, 5, 2, 5}
Output : 5``````

## Python3

``````# Python program to find the element occurring
# odd number of times

# function to find the element occurring odd
# number of times
def getOddOccurrence(arr, arr_size):

for i in range ( 0 , arr_size):
count = 0
for j in range ( 0 , arr_size):
if arr[i] = = arr[j]:
count + = 1

if (count % 2 ! = 0 ):
return arr[i]

return - 1

# driver code
arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ]
n = len (arr)
print (getOddOccurrence(arr, n))

# This code has been contributed by
# Smitha Dinesh Semwal``````

``5``

## python

``````# Python program to find the element occurring odd number of times

def getOddOccurrence(arr):

# Initialize result
res = 0

# Traverse the array
for element in arr:
# XOR with the result
res = res ^ element

return res

# Test array
arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ]

print "% d" % getOddOccurrence(arr)``````

``5``

• 回顶