You are viewing a single comment's thread. Return to all 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 }
Seems like cookies are disabled on this browser, please enable them to open this website
Frequency Queries
You are viewing a single comment's thread. Return to all comments →
Swift Solution: