Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given an array of positive integers, write a function which returns
- // all the unique pairs which add (equal) up to 100.
- const sampleData = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 50, 51, 49, 51];
- // sampleOutput = [[1,99], [0,100], [10,90], [51,49], [50,50]];
- function pairs ( data ) {
- let ret = [];
- let check = [];
- data.forEach( i => {
- data.forEach( y => {
- if ( i + y === 100 ) {
- const one = `${i} ${y}`;
- const two = `${y} ${i}`;
- if ( !check.includes( one ) && !check.includes( two ) ) {
- check.push( one );
- ret.push( [ i, y ] );
- }
- }
- } );
- } );
- return ret;
- }
Add Comment
Please, Sign In to add comment