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 97533d2

Browse files
add multiset implementation (#11)
* add multiset implementation * add multiset implementation
1 parent 31363b5 commit 97533d2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

‎Algorithms/Multiset.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.HashMap;
2+
3+
/* @author: jaswant developer.jaswant@gmail.com
4+
* @algorithm: hashing
5+
* @use: holding frequency map, similar to multiset in c++
6+
*/
7+
class MultiSet<K> {
8+
private HashMap<K, Integer> multiSet = new HashMap<K, Integer>();
9+
10+
int get(K key){
11+
return multiSet.getOrDefault(key, 0);
12+
}
13+
14+
void add(K key){
15+
multiSet.put(key, get(key)+ 1);
16+
}
17+
18+
void remove(K key){
19+
int freq = get(key);
20+
if(freq == 1){
21+
multiSet.remove(key);
22+
}else{
23+
multiSet.put(key, freq - 1);
24+
}
25+
}
26+
27+
@Override
28+
public String toString(){
29+
return multiSet.toString();
30+
}
31+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ In This Repository, I have written some of the important Algorithms and Data Str
4141
| [Trie / Prefix Tree](https://github.com/joney000/Java-Competitive-Programming/blob/master/Algorithms/Trie.java)| O(N * L), O(N * L)| if there are N strings of L size, per query time(Prefix information) = O(L)
4242
| [LIS - Longest Increasing Subsequence](https://github.com/joney000/Java-Competitive-Programming/blob/master/Algorithms/LIS_nLOGn.java)| O(N * log(N)), O(N)
4343
| [Priority Queue](https://github.com/joney000/Java-Competitive-Programming/blob/master/Algorithms/PriorityQueue.java)| O(log(N)), O(N) | N = no of objects in the queue. peek: O(1), poll/add: O(log n)
44+
| [Multiset](https://github.com/joney000/Java-Competitive-Programming/blob/master/Algorithms/Multiset.java)| O(log(N)), O(N) | N = no of objects in the multiset. get/add: O(log n) and Average Case O(1)
4445

4546
## Contributions
4647

0 commit comments

Comments
(0)

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