Frequency Queries

Sort by

recency

|

843 Discussions

|

  • + 0 comments

    What's wrong here, which corner case may I be missing? (suceeds with some of them)

    def freqQuery(queries):
        '''
        Function to conduct the operation as defined in the query list upon an array
    
        Inputs:
        queries         - [List(tuples)] List containing tuples with (actionType, value)
    
        Returns:
        None
        '''
        myArray = []
        outputArray = []
        
        for query in queries:
            queryValue = query[1]
            
            # debug
            print(f"myArray: {myArray}")
            print(f"query type: {query[0]}, query value: {queryValue}")
            
            # Check type of query and perform mentioned action
            match query[0]:
                case 1:
                    # Append to list
                    myArray.append(queryValue)
                case 2:
                    # Convert to set and check if the element exists
                    mySet = set(myArray)
                    if mySet.intersection([queryValue]):
                        myArray.remove(queryValue)
                case 3:
                    # Create a dictinoary of frequencies
                    myDict = dict(())
                    for element in myArray:
                        count = 0
                        if myDict.get(element) == None:
                            myDict.update({element: count+1})
                        else:
                            myDict.update({element: myDict.get(element)+ 1})
    
                    # Convert the dict to list for easy iteration
                    frequencies = list(myDict.items())
                    
                    # Check for matching or NO frequencies
                    if not frequencies:
                        outputArray.append(0)
                    else:
                        for index, frequency in enumerate(frequencies):
                            if frequency[1] == queryValue:
                                outputArray.append(1)
                                break
                            else:
                                outputArray.append(0)
                                break
                                 
        return outputArray
    
  • + 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;
    
            }
    
  • + 0 comments

    approximately O(q) using unordered_map, which are hash objects

    each query takes constant time if there's no hash collison

    vector<int> freqQuery(const vector<vector<int>>& queries) {
        unordered_map<int, int> freq, freqCount;
        vector<int> answer;
        for (int i = 0; i < queries.size(); ++i) {
            if (queries[i][0] == 1) {
                auto it = freq.find(queries[i][1]);
                if (it != freq.end()) --freqCount[it->second];
                int t = ++freq[queries[i][1]];
                ++freqCount[t];
            }
            else if (queries[i][0] == 2) {
                auto it = freq.find(queries[i][1]);
                if (it != freq.end() and it->second != 0) {
                    --freqCount[it->second];
                    --it->second;
                    ++freqCount[it->second];
                }
            }
            else if (queries[i][0] == 3) {
                auto it = freqCount.find(queries[i][1]);
                if (it != freqCount.end() and it->second != 0) answer.push_back(1);
                else answer.push_back(0);
            }
        }
        return answer;
    }
    
  • + 0 comments
    def freqQuery(queries):
        freq_map = {}
        result = []
         
        for query in queries:
            operation, element = query
            
            if operation == 1:
                freq_map.setdefault(element, 0)
                freq_map[element] += 1
                
            elif operation == 2 and freq_map.get(element):
                freq_map[element] -= 1
                
            elif operation == 3:
                matched = element in freq_map.values()
                result.append(1) if matched else result.append(0)
                
        return result
    
  • + 0 comments

    Swift Solution:

    func freqQuery(queries: [[Int]]) -> [Int] {
        var dictOfValueAndFreq = [Int: Int]()
        var countOfFreq = [Int: Int]()
        
        var res: [Int] = []
        for query in queries {
            let q = query[0]
            let value = query[1]
            switch q {
            case 1:
                let oldFreq = dictOfValueAndFreq[value, default: 0]
                let newFreq = oldFreq + 1
                dictOfValueAndFreq[value, default: 0] = newFreq
                if oldFreq > 0 {
                    countOfFreq[oldFreq, default: 0] -= 1
                }
                countOfFreq[newFreq, default: 0] += 1
            case 2:
                if let oldFreq = dictOfValueAndFreq[value],
                   oldFreq > 0 {
                    let newFreq = oldFreq - 1
                    dictOfValueAndFreq[value, default: 0] = newFreq
                    countOfFreq[oldFreq, default: 0] -= 1
                    if newFreq > 0 {
                        countOfFreq[newFreq, default: 0] += 1
                    }
                }
            default:
                if countOfFreq[value] ?? 0 > 0 {
                    res.append(1)
                } else {
                    res.append(0)
                }
            }
        }
        return res
    }