Skip to content

Commit f7de45e

Browse files
committed
add containsKey utility for multiset
1 parent c3e27ba commit f7de45e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Algorithms/Multiset.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
* @use: holding frequency map, similar to multiset in c++
66
*/
77
class MultiSet<K> {
8+
89
private HashMap<K, Integer> multiSet = new HashMap<K, Integer>();
910
private int size;
10-
int get(K key){
11+
12+
public int get(K key){
1113
return multiSet.getOrDefault(key, 0);
1214
}
1315

14-
void add(K key){
16+
public void add(K key){
1517
size++;
1618
multiSet.put(key, get(key)+ 1);
1719
}
1820

19-
void remove(K key){
21+
public void remove(K key){
2022
int freq = get(key);
2123
size--;
2224
if(freq == 1){
@@ -33,6 +35,10 @@ public int size(){
3335
public boolean isEmpty(){
3436
return size == 0;
3537
}
38+
39+
public boolean containsKey(K key){
40+
return multiSet.containsKey(key);
41+
}
3642

3743
@Override
3844
public String toString(){

0 commit comments

Comments
 (0)