We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 02fa897 commit c3e27baCopy full SHA for c3e27ba
Algorithms/Multiset.java
@@ -6,24 +6,34 @@
6
*/
7
class MultiSet<K> {
8
private HashMap<K, Integer> multiSet = new HashMap<K, Integer>();
9
-
+ private int size;
10
int get(K key){
11
return multiSet.getOrDefault(key, 0);
12
}
13
14
void add(K key){
15
+ size++;
16
multiSet.put(key, get(key)+ 1);
17
18
19
void remove(K key){
20
int freq = get(key);
21
+ size--;
22
if(freq == 1){
23
multiSet.remove(key);
24
}else{
25
multiSet.put(key, freq - 1);
26
27
28
+
29
+ public int size(){
30
+ return size;
31
+ }
32
33
+ public boolean isEmpty(){
34
+ return size == 0;
35
36
37
@Override
38
public String toString(){
39
return multiSet.toString();
0 commit comments