Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e4abf0

Browse files
Add implementation for Majority Element using HashMap
1 parent bdbcdf2 commit 7e4abf0

File tree

2 files changed

+194
-6
lines changed

2 files changed

+194
-6
lines changed

‎Hashing/HashMap/HashMapCode.java

Lines changed: 165 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// the is written by chatgpt , all operation . In The Problem we use the inbuild HashMap.
2+
13

24
import java.util.LinkedList;
35

@@ -13,25 +15,182 @@ public Node(K key, V value) {
1315
}
1416
}
1517

16-
private int n;
17-
private int N;
18+
private int n;// n-> nodes
19+
private int N;// N-> buckets
1820
private LinkedList<Node> buckets[];
1921

2022
//constructor of HashMap
23+
@SuppressWarnings("unchecked")
2124
public HashMap(){
2225
this.N = 4;
2326
this.buckets = new LinkedList[4];
2427
for (int i = 0; i < N; i++) {
2528
this.buckets[i] = new LinkedList<>();
2629
}
2730
}
28-
public void put(K key, V value){
29-
31+
// Hash function to get bucket index
32+
private int hashFunction(K key) {
33+
int hc = key.hashCode();
34+
return Math.abs(hc) % N;
35+
}
36+
37+
// Search for a key in the bucket and return its index
38+
private int searchInLL(K key, int bi) {
39+
LinkedList<Node> ll = buckets[bi];
40+
int di = 0;
41+
for (int i = 0; i < ll.size(); i++) {
42+
Node node = ll.get(i);
43+
if (node.key.equals(key)) {
44+
return di;
45+
}
46+
di++;
47+
}
48+
return -1;
49+
}
50+
51+
// Rehash the HashMap when load factor exceeds threshold
52+
@SuppressWarnings("unchecked")
53+
private void rehash() {
54+
LinkedList<Node> oldBuckets[] = buckets;
55+
buckets = new LinkedList[N * 2];
56+
N = 2 * N;
57+
for (int i = 0; i < buckets.length; i++) {
58+
buckets[i] = new LinkedList<>();
59+
}
60+
61+
// Transfer all nodes to new buckets
62+
n = 0;
63+
for (int i = 0; i < oldBuckets.length; i++) {
64+
LinkedList<Node> ll = oldBuckets[i];
65+
for (int j = 0; j < ll.size(); j++) {
66+
Node node = ll.remove();
67+
put(node.key, node.value);
68+
}
69+
}
70+
}
71+
72+
// Put operation - Insert or update key-value pair
73+
public void put(K key, V value) {
74+
int bi = hashFunction(key);
75+
int di = searchInLL(key, bi);
76+
77+
if (di != -1) {
78+
// Key exists, update value
79+
Node node = buckets[bi].get(di);
80+
node.value = value;
81+
} else {
82+
// Key doesn't exist, add new node
83+
buckets[bi].add(new Node(key, value));
84+
n++;
85+
}
86+
87+
double lambda = (double) n / N;
88+
if (lambda > 2.0) {
89+
rehash();
90+
}
91+
}
92+
93+
// Check if key exists in HashMap
94+
public boolean containsKey(K key) {
95+
int bi = hashFunction(key);
96+
int di = searchInLL(key, bi);
97+
return di != -1;
98+
}
99+
100+
// Get value for a given key
101+
public V get(K key) {
102+
int bi = hashFunction(key);
103+
int di = searchInLL(key, bi);
104+
105+
if (di != -1) {
106+
Node node = buckets[bi].get(di);
107+
return node.value;
108+
} else {
109+
return null;
110+
}
111+
}
30112

113+
// Remove key-value pair from HashMap
114+
public V remove(K key) {
115+
int bi = hashFunction(key);
116+
int di = searchInLL(key, bi);
117+
118+
if (di != -1) {
119+
Node node = buckets[bi].remove(di);
120+
n--;
121+
return node.value;
122+
} else {
123+
return null;
124+
}
31125
}
32126

33-
public boolean containsKey(K key){
34-
return false;
127+
// Get all keys in the HashMap
128+
public java.util.ArrayList<K> keySet() {
129+
java.util.ArrayList<K> keys = new java.util.ArrayList<>();
130+
for (int i = 0; i < buckets.length; i++) {
131+
LinkedList<Node> ll = buckets[i];
132+
for (Node node : ll) {
133+
keys.add(node.key);
134+
}
135+
}
136+
return keys;
35137
}
138+
139+
// Check if HashMap is empty
140+
public boolean isEmpty() {
141+
return n == 0;
142+
}
143+
144+
// Get the size of HashMap
145+
public int size() {
146+
return n;
147+
}
148+
149+
// Print the HashMap for debugging
150+
public void printHashMap() {
151+
System.out.println("HashMap contents:");
152+
for (int i = 0; i < buckets.length; i++) {
153+
System.out.print("Bucket " + i + ": ");
154+
LinkedList<Node> ll = buckets[i];
155+
for (Node node : ll) {
156+
System.out.print("(" + node.key + "," + node.value + ") ");
157+
}
158+
System.out.println();
159+
}
160+
}
161+
}
162+
163+
// Main method for testing
164+
public static void main(String[] args) {
165+
HashMap<String, Integer> hm = new HashMap<>();
166+
167+
// Testing put operation
168+
hm.put("India", 100);
169+
hm.put("China", 150);
170+
hm.put("US", 50);
171+
hm.put("Nepal", 5);
172+
173+
System.out.println("Size: " + hm.size());
174+
hm.printHashMap();
175+
176+
// Testing get operation
177+
System.out.println("\nGet 'India': " + hm.get("India"));
178+
System.out.println("Get 'China': " + hm.get("China"));
179+
System.out.println("Get 'Pakistan': " + hm.get("Pakistan"));
180+
181+
// Testing containsKey operation
182+
System.out.println("\nContains 'India': " + hm.containsKey("India"));
183+
System.out.println("Contains 'Pakistan': " + hm.containsKey("Pakistan"));
184+
185+
// Testing remove operation
186+
System.out.println("\nRemoving 'China': " + hm.remove("China"));
187+
System.out.println("Size after removal: " + hm.size());
188+
hm.printHashMap();
189+
190+
// Testing keySet operation
191+
System.out.println("\nAll keys: " + hm.keySet());
192+
193+
// Testing isEmpty
194+
System.out.println("Is empty: " + hm.isEmpty());
36195
}
37196
}

‎Hashing/Question/MajorityElement.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import java.util.HashMap;
3+
4+
public class MajorityElement {
5+
public static void main(String[] args) {
6+
int nums [] = {1,2,3,1,4,1,5,1,1,1,6,1};
7+
majority_element(nums);
8+
}
9+
10+
public static void majority_element(int num[]){
11+
HashMap <Integer,Integer> map = new HashMap<>();
12+
int size = num.length;
13+
14+
for(int i=0; i<size; i++){
15+
if(map.containsKey(num[i])){
16+
map.put(num[i],map.get(num[i])+1);
17+
}else{
18+
map.put(num[i],1);
19+
}
20+
21+
}
22+
for (int key : map.keySet()) {
23+
if(map.get(key) > size/3){
24+
System.out.println(key);
25+
}
26+
}
27+
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /