classFrequencyTracker:def__init__(self):self.cnt=defaultdict(int)self.freq=defaultdict(int)defadd(self,number:int)->None:self.freq[self.cnt[number]]-=1self.cnt[number]+=1self.freq[self.cnt[number]]+=1defdeleteOne(self,number:int)->None:ifself.cnt[number]:self.freq[self.cnt[number]]-=1self.cnt[number]-=1self.freq[self.cnt[number]]+=1defhasFrequency(self,frequency:int)->bool:returnself.freq[frequency]>0# Your FrequencyTracker object will be instantiated and called as such:# obj = FrequencyTracker()# obj.add(number)# obj.deleteOne(number)# param_3 = obj.hasFrequency(frequency)
classFrequencyTracker{privateMap<Integer,Integer>cnt=newHashMap<>();privateMap<Integer,Integer>freq=newHashMap<>();publicFrequencyTracker(){}publicvoidadd(intnumber){freq.merge(cnt.getOrDefault(number,0),-1,Integer::sum);cnt.merge(number,1,Integer::sum);freq.merge(cnt.get(number),1,Integer::sum);}publicvoiddeleteOne(intnumber){if(cnt.getOrDefault(number,0)>0){freq.merge(cnt.get(number),-1,Integer::sum);cnt.merge(number,-1,Integer::sum);freq.merge(cnt.get(number),1,Integer::sum);}}publicbooleanhasFrequency(intfrequency){returnfreq.getOrDefault(frequency,0)>0;}}/** * Your FrequencyTracker object will be instantiated and called as such: * FrequencyTracker obj = new FrequencyTracker(); * obj.add(number); * obj.deleteOne(number); * boolean param_3 = obj.hasFrequency(frequency); */
classFrequencyTracker{public:FrequencyTracker(){}voidadd(intnumber){freq[cnt[number]]--;cnt[number]++;freq[cnt[number]]++;}voiddeleteOne(intnumber){if(cnt[number]){freq[cnt[number]]--;cnt[number]--;freq[cnt[number]]++;}}boolhasFrequency(intfrequency){returnfreq[frequency]>0;}private:unordered_map<int,int>cnt;unordered_map<int,int>freq;};/** * Your FrequencyTracker object will be instantiated and called as such: * FrequencyTracker* obj = new FrequencyTracker(); * obj->add(number); * obj->deleteOne(number); * bool param_3 = obj->hasFrequency(frequency); */
typeFrequencyTrackerstruct{cntmap[int]intfreqmap[int]int}funcConstructor()FrequencyTracker{returnFrequencyTracker{map[int]int{},map[int]int{}}}func(this*FrequencyTracker)Add(numberint){this.freq[this.cnt[number]]--this.cnt[number]++this.freq[this.cnt[number]]++}func(this*FrequencyTracker)DeleteOne(numberint){ifthis.cnt[number]>0{this.freq[this.cnt[number]]--this.cnt[number]--this.freq[this.cnt[number]]++}}func(this*FrequencyTracker)HasFrequency(frequencyint)bool{returnthis.freq[frequency]>0}/** * Your FrequencyTracker object will be instantiated and called as such: * obj := Constructor(); * obj.Add(number); * obj.DeleteOne(number); * param_3 := obj.HasFrequency(frequency); */
classFrequencyTracker{privatecnt:Map<number,number>;privatefreq:Map<number,number>;constructor(){this.cnt=newMap();this.freq=newMap();}add(number:number):void{constf=this.cnt.get(number)||0;this.freq.set(f,(this.freq.get(f)||0)-1);this.cnt.set(number,f+1);this.freq.set(f+1,(this.freq.get(f+1)||0)+1);}deleteOne(number:number):void{constf=this.cnt.get(number)||0;if(f>0){this.freq.set(f,(this.freq.get(f)||0)-1);this.cnt.set(number,f-1);this.freq.set(f-1,(this.freq.get(f-1)||0)+1);}}hasFrequency(frequency:number):boolean{return(this.freq.get(frequency)||0)>0;}}/** * Your FrequencyTracker object will be instantiated and called as such: * var obj = new FrequencyTracker() * obj.add(number) * obj.deleteOne(number) * var param_3 = obj.hasFrequency(frequency) */
usestd::collections::HashMap;structFrequencyTracker{cnt:HashMap<i32,i32>,freq:HashMap<i32,i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implFrequencyTracker{fnnew()->Self{FrequencyTracker{cnt:HashMap::new(),freq:HashMap::new(),}}fnadd(&mutself,number:i32){letcount=self.cnt.entry(number).or_insert(0);letprev_freq=self.freq.entry(*count).or_insert(0);*prev_freq-=1;*count+=1;letnew_freq=self.freq.entry(*count).or_insert(0);*new_freq+=1;}fndelete_one(&mutself,number:i32){ifletSome(count)=self.cnt.get_mut(&number){if*count>0{ifletSome(prev_freq)=self.freq.get_mut(count){*prev_freq-=1;}*count-=1;ifletSome(new_freq)=self.freq.get_mut(count){*new_freq+=1;}}}}fnhas_frequency(&self,frequency:i32)->bool{self.freq.get(&frequency).map_or(false,|&freq|freq>0)}}