Skip to content

Commit

Permalink
finished the pairs-count function
Browse files Browse the repository at this point in the history
  • Loading branch information
Afek-Sakaju committed Jun 21, 2023
1 parent b730920 commit 5900de4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion code-questions/array/__tests__/pairs-count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { getPairsCount } = require('../pairs-count');

describe('pairs-count tests', () => {
test.each([
// [[1, 5, 7, 1], 6, 2],
[[1, 5, 7, 1], 6, 2],
[[1, 1, 1, 1], 2, 6],
[[3, 4], 7, 1],
[[-1, -2, 3, 4], -3, 1],
Expand Down
8 changes: 4 additions & 4 deletions code-questions/array/pairs-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ module.exports.getPairsCount = function (arr, k) {
});

arr.forEach((num) => {
if (k - num === num) {
if (k - num === num) {
if (obj[k - num] > 1) {
pairsCount += obj[k - num];
obj[num] --;
pairsCount += obj[k - num] - 1;
obj[num]--;
}
} else {
if (obj[k - num] > 0) {
Expand All @@ -51,6 +51,6 @@ module.exports.getPairsCount = function (arr, k) {
}
}
});

return pairsCount;
};

0 comments on commit 5900de4

Please sign in to comment.