Skip to content

Commit a5b67b6

Browse files
author
Mia von Steinkirch
committed
more ex
1 parent 1b336bf commit a5b67b6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

interview_cake/math/find_dup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/python
2+
3+
"""
4+
Find a duplicate
5+
We have a list of integers, where:
6+
The integers are in the range 1..n1..n
7+
The list has a length of n+1n+1
8+
It follows that our list has at least one integer which appears at least twice. But it may have several duplicates, and each duplicate may appear more than twice.
9+
10+
Write a function which finds an integer that appears more than once in our list. (If there are multiple duplicates, you only need to find one of them.)
11+
"""
12+
13+
def find_dups(num_list):
14+
num_dict = {}
15+
for n in num_list:
16+
if n in num_dict.keys():
17+
num_dict[n] += 1
18+
else:
19+
num_dict[n] = 1
20+
21+
for k,v in num_dict.items():
22+
if v > 1:
23+
print "dup is {}".format(k)
24+
25+
def find_dups_set(num_list):
26+
27+
num_set = set()
28+
29+
for n in num_list:
30+
if n in num_set:
31+
print n
32+
else:
33+
num_set.add(n)
34+
35+
36+
num_list = [6,1,3,7,6,4,5,2,8,5,6,6,7]
37+
find_dups(num_list)
38+
find_dups_set(num_list)

0 commit comments

Comments
 (0)