# LeetCode: Finding Pairs With A Certain Sum Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * @param {number[]} nums13 * @param {number[]} nums24 */5var FindSumPairs = function (nums1, nums2) {6  this.nums1 = nums17  this.nums2 = nums28  this.occurences2 = nums2.reduce(9    (acc, el) => acc.set(el, (acc.get(el) || 0) + 1),10    new Map()11  )12}13
14/**15 * @param {number} index16 * @param {number} val17 * @return {void}18 */19FindSumPairs.prototype.add = function (index, val) {20  const num = this.nums2[index]21  const newNum = num + val22  this.occurences2.set(num, this.occurences2.get(num) - 1)23  this.occurences2.set(newNum, (this.occurences2.get(newNum) || 0) + 1)24  this.nums2[index] = newNum25}26
27/**28 * @param {number} tot29 * @return {number}30 */31FindSumPairs.prototype.count = function (tot) {32  let res = 033  for (const num1 of this.nums1) {34    res += this.occurences2.get(tot - num1) || 035  }36  return res37}38
39/**40 * Your FindSumPairs object will be instantiated and called as such:41 * var obj = new FindSumPairs(nums1, nums2)42 * obj.add(index,val)43 * var param_2 = obj.count(tot)44 */```

leetcode

hash table

## Next Post

LeetCode: Minimum Number Of Swaps To Make The Binary String Alternating

May 16, 2021

Search Posts