3
// output: corresponding occurrence-based dictionary7
arr.forEach((val) => (keyOccDict[val] = (keyOccDict[val] || 0) + 1));12
var FindSumPairs = function (nums1, nums2) {16
// maintain mapping between distinct number and occurrence for array A17
this.dictA = Counter(this.arrA);19
// maintain mapping between distinct number and occurrence for array B20
this.dictB = Counter(this.arrB);25
FindSumPairs.prototype.add = function (index, val) {26
// update mapping for arrB28
let old_value = this.arrB[index];29
this.arrB[index] += val;30
let new_value = this.arrB[index];32
this.dictB[old_value] = this.dictB[old_value] - 1;33
this.dictB[new_value] = (this.dictB[new_value] || 0) + 1;38
FindSumPairs.prototype.count = function (tot) {42
Find the method count of a + b = total,43
where a comes from array A, and b comes from array B45
apply the concept learned from Leetcode #1 Two sum46
a + b = total <=> b = total - a48
speed-up by dictionary53
for (const [a, occ_a] of Object.entries(this.dictA)) {56
counter += occ_a * (this.dictB[b] || 0);