0

I have an array with some numbers

[0, 0.1, 0.2, 0.3, 0.6, 0.8, 1]

how do I remove numbers that are close to each other less than or equal to 0.2 threshold and put the average of those values instead, so the above array becomes

[0.2, 0.8]

0.2 because [0, 0.1, 0.2, 0.3] are close to each other so they become (0 + 0.1 + 0.2 + 0.3) / 4 = 0.2

0.8 because [0.6, 0.8, 1] are close to each other so they become (0.6, 0.8, 1) / 2 = 0.8

derpirscher
  • 14,418
  • 3
  • 18
  • 35
eguneys
  • 6,028
  • 7
  • 31
  • 63

1 Answers1

1

You could do this by iterating over every element of the array and keeping a temporary close array. If close is empty, or the current element and the last element in close's difference is less than the threshold, then you can add it to close. If not, then average close and add it to the output array. Note that floating point math gets a bit weird here, you can use this rounding trick.

const fixFloat = n => parseFloat(n.toPrecision(12))

function average(arr) {
  let sum = 0;
  for (const el of arr) sum += el;
  return fixFloat(sum / arr.length);
}

function closeRemove(arr, threshold) {
  arr = [...arr].sort()
  const res = [];
  let close = [];
  for (let i = 0; i < arr.length; i++) {
    const diff = fixFloat(arr[i] - close[close.length-1]);
    if (close.length == 0 || diff <= threshold) {
      close.push(arr[i])
    } else {
      res.push(average(close));
      close = [arr[i]];
    }
  }
  res.push(average(close));
  return res;
}

const arr = [0, 0.1, 0.2, 0.3, 0.6, 0.8, 1];
console.log(closeRemove(arr, 0.2));

Note that in your question, the numbers 0, 0.1, 0.2, 0.3 all have a difference of less than 0.2, so they should be averaged together. Their average is 1.5, not 2.

Michael M.
  • 10,486
  • 9
  • 18
  • 34