-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashing.py
More file actions
56 lines (37 loc) · 1.05 KB
/
hashing.py
File metadata and controls
56 lines (37 loc) · 1.05 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
51
52
53
54
#Array hashing, when values are in limited range
# faster than dict ( no hashing overhead)
mylist=list(map(int,input("Enter numbers :").split(",")))
queries= list(map(int,input("Enter queries :").split(",")))
myhash=[0]*101
for num in mylist:
myhash[num]+=1
for q in queries:
print(myhash[q],end=" ") # TC
# Basic hashing using dictionary ( frequency count )
arr = list(map(int,input("Enter numbers :").split(",")))
frequency = {}
for num in arr :
frequency[num] = frequency.get(num,0) +1 #Tc o(n)
print(frequency)
# Hashing with queries
arr=[1,2,3,4,5,6,3,2,]
queries =[1,3,2]
fre={}
for num in arr:
fre[num] = fre.get(num,0)+1
for q in queries:
que= fre.get(q,0)
print(q, "-" ,que) # tc (n+q)
#Hashing for "check existence only" (Set )
arr=[1,2,3,4,5,6,3,2,]
s = set(arr)
print(20 in s) # False Tc (1)
#first non repeating element
arr = [4, 5, 1, 2, 0, 4]
fre = {}
for num in arr :
fre[num]= fre.get(num,0)+1
for num in arr :
if fre[num] == 1:
print("first non_repeating :", num )
break