Skip to content

Commit c3e27ba

Browse files
committed
add multiset size and isempty methods
1 parent 02fa897 commit c3e27ba

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Algorithms/Multiset.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,34 @@
66
*/
77
class MultiSet<K> {
88
private HashMap<K, Integer> multiSet = new HashMap<K, Integer>();
9-
9+
private int size;
1010
int get(K key){
1111
return multiSet.getOrDefault(key, 0);
1212
}
1313

1414
void add(K key){
15+
size++;
1516
multiSet.put(key, get(key)+ 1);
1617
}
1718

1819
void remove(K key){
1920
int freq = get(key);
21+
size--;
2022
if(freq == 1){
2123
multiSet.remove(key);
2224
}else{
2325
multiSet.put(key, freq - 1);
2426
}
2527
}
26-
28+
29+
public int size(){
30+
return size;
31+
}
32+
33+
public boolean isEmpty(){
34+
return size == 0;
35+
}
36+
2737
@Override
2838
public String toString(){
2939
return multiSet.toString();

0 commit comments

Comments
 (0)