Skip to content

Commit d0787a3

Browse files
authored
Merge pull request #1382 from Maheshwari-Love/add/hashtable
added the Hash table search algorithm in searching techniques
2 parents ad6dfd3 + c9f82e4 commit d0787a3

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

assets/Hash-Table-Search.png

473 KB
Loading
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
id: Hash-Table-Search
3+
title: Hash Table Search (Geeks for Geeks)
4+
sidebar_label: Hash Table Search
5+
tags:
6+
- Beginner
7+
- Search Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the Hash Table Search problem on Geeks for Geeks."
15+
---
16+
17+
## What is Hash Table Search?
18+
19+
Hash Table Search is a search operation performed on a hash table, a data structure that stores key-value pairs. It allows for efficient insertion, deletion, and search operations in average O(1) time complexity.
20+
21+
## Algorithm for Hash Table Search
22+
23+
1. Compute the hash code of the target key using the hash function.
24+
2.Use the hash code to find the index of the target key in the hash table.
25+
3. If the key is found at the computed index, return the associated value.
26+
4. If the key is not found at the computed index, return null.
27+
28+
## How does Hash Table Search work?
29+
30+
- It begins by computing the hash code of the target key.
31+
- The hash code is then used to determine the index in the hash table where the key-value pair should be stored or searched.
32+
- If the key exists at the computed index, the corresponding value is returned.
33+
- If the key does not exist at the computed index, the search returns null.
34+
35+
36+
37+
![Example for AVL Tree Search(GFG)](../../assets/Hash-Table-Search.png)
38+
39+
## Problem Description
40+
41+
Given a hash table and a target key, implement the Hash Table Search algorithm to find the value associated with the target key in the table. If the key is not present, return null.
42+
43+
## Examples
44+
45+
**Example 1:**
46+
```
47+
Input:
48+
Hash Table: {1: 'one', 2: 'two', 3: 'three'}
49+
Target Key: 2
50+
Output: 'two'
51+
52+
```
53+
54+
**Example 2:**
55+
```
56+
Input:
57+
Hash Table: {1: 'one', 2: 'two', 3: 'three'}
58+
Target Key: 4
59+
Output: null
60+
```
61+
62+
## Your Task:
63+
64+
You don't need to read input or print anything. Complete the function hashTableSearch() which takes a hash table and a target key as input parameters and returns the value associated with the target key. If the target key is not present, return null.
65+
66+
Expected Time Complexity: $O(1)$
67+
Expected Auxiliary Space: $O(1)$
68+
69+
## Constraints
70+
71+
- $1 <= Number of key-value pairs <= 10^5$
72+
- Keys are integers and unique.
73+
- Values are strings.
74+
75+
## Implementation
76+
77+
<Tabs>
78+
79+
<TabItem value="Java label="Java">
80+
<SolutionAuthor name="@ngmuraqrdd"/>
81+
```java
82+
83+
import java.util.HashMap;
84+
85+
public class HashTableSearch {
86+
public static String hashTableSearch(HashMap<Integer, String> hashTable, int target) {
87+
return hashTable.getOrDefault(target, null);
88+
}
89+
90+
public static void main(String[] args) {
91+
HashMap<Integer, String> hashTable = new HashMap<>();
92+
hashTable.put(1, "one");
93+
hashTable.put(2, "two");
94+
hashTable.put(3, "three");
95+
96+
int target = 2;
97+
System.out.println(hashTableSearch(hashTable, target)); // Output: 'two'
98+
99+
target = 4;
100+
System.out.println(hashTableSearch(hashTable, target)); // Output: null
101+
}
102+
}
103+
104+
</TabItem>
105+
106+
<TabItem value="C++" label="C++">
107+
<SolutionAuthor name="@ngmuraqrdd"/>
108+
```cpp
109+
#include <iostream>
110+
#include <unordered_map>
111+
112+
std::string hashTableSearch(const std::unordered_map<int, std::string>& hashTable, int target) {
113+
auto it = hashTable.find(target);
114+
if (it != hashTable.end()) {
115+
return it->second;
116+
}
117+
return "null";
118+
}
119+
120+
int main() {
121+
std::unordered_map<int, std::string> hashTable = {{1, "one"}, {2, "two"}, {3, "three"}};
122+
123+
int target = 2;
124+
std::cout << hashTableSearch(hashTable, target) << std::endl; // Output: 'two'
125+
126+
target = 4;
127+
std::cout << hashTableSearch(hashTable, target) << std::endl; // Output: null
128+
129+
return 0;
130+
}
131+
132+
133+
</TabItem>
134+
```
135+
136+
</Tabs>
137+
138+
## Complexity Analysis
139+
140+
- **Time Complexity**:$O(1)$, as hash table operations (insertion, deletion, and search) typically have constant time complexity.
141+
142+
- **Space Complexity**:$O(1)$, as no extra space is required apart from the input hash table.
143+
144+
## Advantages and Disadvantages
145+
146+
**Advantages:**
147+
- Provides efficient search, insertion, and deletion operations.
148+
- Useful for applications requiring fast lookups and quick data retrieval.
149+
150+
**Disadvantages:**
151+
- May require more memory compared to other data structures due to hashing and potential collisions.
152+
- Performance can degrade if the hash function leads to many collisions.
153+
References
154+
155+
## References
156+
157+
- **GFG Problem:** [GFG Problem](https://www.geeksforgeeks.org/hash-table-data-structure/)
158+
- **Author's Geeks for Geeks Profile:** [MuraliDharan](https://www.geeksforgeeks.org/user/ngmuraqrdd/)
159+

0 commit comments

Comments
 (0)