DEV Community

Discussion on: Daily Challenge #215 - Difference of 2

Collapse
 
sabbin profile image
Sabin Pandelovitch • Edited

JS solution with reduce

const pairDifference = arr =>
  arr.sort((a,b)=>a-b).reduce((acc, val, i, oa) => {
    for (let j = i + 1; j < oa.length; j++) {
      if (Math.abs(val - oa[j]) === 2) {
        acc.push([val, oa[j]]);
      }
    }
    return acc;
  }, []);