-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.py
85 lines (81 loc) · 1.92 KB
/
hashing.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# word = dict()
# word['Web Dev'] = 1
# word['Andriod'] = 1
# word['Data Science'] = 1
# word['Web Dev'] = word['Web Dev'] + 1
# # print(word['Andriod'])
# counts = dict()
# domain = ['Web Dev','Andriod','Data Science']
# for jobs in domain:
# counts[jobs] = counts.get(jobs,0) + 1
# print(counts)
# Q1 - Write a program to count the number of times each word occurs in an array
# arr = [2,7,9,31,7,8,9,2,7]
# hmap = {}
# for i in arr:
# if i in hmap:
# hmap[i]+=1
# else:
# hmap[i]=1
# for k,v in hmap.items():
# print(k,v)
# print(len(hmap))
# Q2 - Write a program to displat the first non-repeating character in an array
# nums = [20,1,7,6,1,20,3,8,20,3,8,7]
# hmap = {}
# for i in nums:
# if i in hmap:
# hmap[i]+=1
# else:
# hmap[i]= 1
# for k,v in hmap.items():
# if v == 1:
# print(k)
# break
# Q3 - Write a program to find the common elements between two arrays
# a =[ 3,2,15,4,7,8,2]
# b =[4,9,11,6,2]
# hmap = {}
# res = []
# for i in a:
# if i in hmap:
# hmap[i]+=1
# else:
# hmap[i] = 1
# for j in b:
# if j in hmap:
# res.append(j)
# del hmap[j]
# print(res)
# nums1 = set(a) # Use set to remove duplicates
# ans = []
# for num in b:
# if num in nums1:
# ans.append(num)
# nums1.remove(num)
# print(ans)
# Q4 - Write a program to check if arr[i] + arr[j] = k and i != j
arr = [1,3,2,3,8,9]
k = 4
# for i in range(len(arr)): ## tc = O(n^2)
# for j in range(i,len(arr)):
# if arr[i] + arr[j] == k and i != j:
# print(arr[i],arr[j])
# print(-1)
# hset = set() ## tc = O(n) using hashset
# for i in arr:
# r = k - i
# if r in hset:
# print(i,r)
# break
# else:
# hset.add(i)
# hmap = {} ## tc = O(n) using hashmap
# for i in arr:
# print(i)
# r = k - i
# if r in hmap:
# print(i,r)
# break
# else:
# hmap[i] = 1