###### tags: `LeetCode` `HackRank` # HackRank Frequency Queries ## Goal 按照輸入的array做事情 Operation ( a , b) a = 1 , 陣列新增1個b a = 2 , 陣列刪除1個b a = 3 , 查看陣列中是否有b個個數的元素,有印出1,沒有印出0 ## Solution ``` func freqQuery(queries [][]int32) []int32 { m := make(map[int32]int32) //數字b出現的次數 1 1 1 2 2 2 3 3 4 [1]=3,[2]=3,[3]=2,[4]=1 times := make(map[int32]int32) // 儲存出現的次數 [3]=2,[2]=1,[1]=1 ans := []int32{} for i:=0 ; i< len(queries) ; i++ { first := queries[i][0] second := queries[i][1] switch(first) { case 1: m[second]++ times[m[second]]++ times[m[second]-1]-- case 2: m[second]-- times[m[second]+1]-- times[m[second]]++ case 3: v ,ok := times[second] if ok && v!=0{ ans = append(ans,1) }else{ ans = append(ans,0) } } } return ans } ```