Skip to content

Commit

Permalink
Merge pull request #158 from Diptigit11/patch-1
Browse files Browse the repository at this point in the history
Create majorityElementHashhmap.java
  • Loading branch information
sujana-kamasany authored Oct 11, 2023
2 parents 1523453 + cb01000 commit 526be11
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Hashmaps/majorityElementHashhmap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;

public class majorityElementHashhmap {
public static void majorityElement(int nums[]) {
HashMap<Integer, Integer> map = new HashMap<>();
int n = nums.length;
for (int i = 0; i < n; i++) {
if (map.containsKey(nums[i])) { //if num is present already
map.put(nums[i], map.get(nums[i]) + 1);
} else { //if num is not present
map.put(nums[i], 1);
}
}
for (int Key : map.keySet()) {
if (map.get(Key) > n / 3) {
System.out.println(" The no. of elements which appeared more than n/3 times is: " + Key);
}
}
}

public static void main(String[] args) {
int nums[] = { 1, 3, 2, 5, 1, 3, 1, 5, 1 };
majorityElement(nums);
}
}

0 comments on commit 526be11

Please sign in to comment.