Frequency Queries

  • + 0 comments

    c#

            public static List<int> freqQuery(List<List<int>> queries)
            {
                Dictionary<long, int> res = new Dictionary<long, int>();
                List<int> ret = new List<int>();
                foreach (List<int> query in queries)
                {
                    switch(query[0])
                    {
                        case 1:
                            if (!res.ContainsKey(query[1]))
                            {
                                res.Add(query[1], 1);
                            } else
                            {
                                res[query[1]]++;
                            }
    
                            break;
                        case 2:
                            if (res.ContainsKey(query[1]))
                            {
                                if (res[query[1]] == 1)
                                {
                                    res.Remove(query[1]);
                                }
                                else
                                {
                                    res[query[1]]--;
                                }
                            }
                            break;
                        case 3:
                            int y = 0;
                            if (res.Values.Contains(query[1])) { y = 1; }
                            ret.Add(y);
                            break;
                    }
                }
    
                return ret;
    
            }