-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind-missing-number.py
More file actions
51 lines (37 loc) · 1.26 KB
/
Find-missing-number.py
File metadata and controls
51 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3.6
'''
Consider an array of non-negative integers. A second array is formed by shuffling the elements of the first array and deleting a random element. Given these two arrays, find which element is missing in the second array.
Here is an example input, the first array is shuffled and the number 5 is removed to construct the second array.
Input:
finder([1,2,3,4,5,6,7],[3,7,2,1,4,6])
Output:
5 is the missing number
'''
def find_mising_number(arr1,arr2):
arr1.sort()
arr2.sort()
for num1,num2 in zip(arr1,arr2):
if num1 != num2:
return num1
return arr1[-1]
print(find_mising_number([1,2,3,4,5,6,7],[7,5,6,2,1,4]))
import collections
def finder2(arr1, arr2):
# Using default dict to avoid key errors
d=collections.defaultdict(int)
# Add a count for every instance in Array 1
for num in arr2:
d[num]+=1
# Check if num not in dictionary
for num in arr1:
if d[num]==0:
return num
# Otherwise, subtract a count
else: d[num]-=1
def finder3(arr1, arr2):
result=0
# Perform an XOR between the numbers in the arrays
for num in arr1+arr2:
result^=num
print (result)
return result