Arrays
Data Types
Algorithms
Logic
Data
Find positive and negative integer pair in the array.
There is only one pair in the array.
Think about the performance.
[1,2,3,-2,4,5,6] -> [-2, 2]
[1,5,7,8,-2,3,-7] -> [-7, 7]
function pairs(arr){
let sorted = arr.sort();
let length = arr.length;
for (let i = 0; i < length; i++) {
if (sorted.includes(arr[i] * -1)) {
return [sorted[i], sorted[i] * -1];
}
}
}
describe("Solution", function() {
it("should return right pair", function() {
Test.assertSimilar(pairs([1, 3, 6, -2, 4, -6]), [-6,6])
Test.assertSimilar(pairs([3, 6, 8, -2, 4, 5, -1, -8]), [-8, 8])
});
});