Skip to content

Commit 4678754

Browse files
Create lonelyInteger
Check out the resources on the page's right side to learn more about bit manipulation. The video tutorial is by Gayle Laakmann McDowell, author of the best-selling interview book Cracking the Coding Interview. Consider an array of integers, , where all but one of the integers occur in pairs. In other words, every element in occurs exactly twice except for one unique element. Given , find and print the unique element. Input Format The first line contains a single integer, , denoting the number of integers in the array. The second line contains space-separated integers describing the respective values in . Constraints It is guaranteed that is an odd number. , where . Output Format Print the unique number that occurs only once in on a new line. Sample Input 0 1 1 Sample Output 0 1 Explanation 0 The array only contains a single , so we print as our answer. Sample Input 1 3 1 1 2 Sample Output 1 2 Explanation 1 We have two 's and one . We print , because that's the only unique element in the array. Sample Input 2 5 0 0 1 2 1 Sample Output 2 2 Explanation 2 We have two 's, two 's, and one . We print , because that's the only unique element in the array.
1 parent f7cf6c1 commit 4678754

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lonelyInteger

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/python
2+
3+
import sys
4+
5+
def lonely_integer(a):
6+
lonelyInteger = a[0]
7+
for i in range(len(a)):
8+
if a.count(a[i]) == 1:
9+
lonelyInteger = a[i]
10+
return lonelyInteger
11+
12+
13+
n = int(raw_input().strip())
14+
a = map(int,raw_input().strip().split(' '))
15+
print lonely_integer(a)

0 commit comments

Comments
 (0)