Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
Find most stable point of two array parallelly

I have two arrays.

let arr1 = [32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50];
let arr2 = [46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97];

Now I want a "stable" index of both arrays. like what is the index when both arrays are most state?

For example from the given array I want.

let index = 8 // this is what i want

Index 8 means from arr1 the ist "44" and from arr2 "78"

Let me define what I mean by stable here.

At least their one before element and one after element's difference is the least of both arrays. (Sorry for my bad explanation.)

Don't flag this question just comment on what you need to know, I will try to explain. [Please]

As recommended by @Nina Scholz

This is what i tried so far [not working]

let arr1 = [32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50];
let arr2 = [46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97];
let diff_arr1 = []
let diff_arr2 = []
let compare_arr = []

for (let i = 0; i < arr1.length; i++) {
    diff_arr1.push(Math.abs(arr1[i + 1] - arr1[i]));
}

for (let i = 0; i < arr2.length; i++) {
    diff_arr2.push(Math.abs(arr2[i + 1] - arr2[i]));
}

for (let i = 0; i < 12; i++) {
    compare_arr.push(Math.abs(diff_arr2[i] - diff_arr1[i]));
}

console.log("Array 1")
console.log("Array 1 diff");
console.log(diff_arr1);

console.log("Array 2")
console.log("Array 2 diff");
console.log(diff_arr2);

console.log('compare_arr')
console.log(compare_arr)
let arr_temp = compare_arr.filter(function (value) {
    return !Number.isNaN(value);
});
console.log(Math.min.apply(Math, arr_temp));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could take the delta of the previous value with actual and actual and next value and take later this delta the get a minimum delta of both deltas.

The result is different from the wanted, because it take the minimum delta of both arrays.

                          v
 32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50
NaN  13  22  15   8   8   7   6   3   5   5 NaN

 46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97
NaN   9   1   2   8  13   7   8  14   8  12 NaN

const
    getDelta = (array, offset) => array
        .map((v, i, a) => Math.abs(v - array[i - 1]) + Math.abs(v - array[i + 1])),
    array1 = [32, 35, 25, 37, 40, 45, 42, 46, 44, 45, 49, 50],
    array2 = [46, 55, 55, 56, 57, 64, 70, 71, 78, 85, 86, 97],
    delta1 = getDelta(array1, 2),
    delta2 = getDelta(array2, 2),
    result = [...array1.keys()]
        .slice(1, -1)
        .reduce((a, b) => Math.abs(delta1[a] - delta2[a]) < Math.abs(delta1[b] - delta2[b])
        ? a
        : b
    );

console.log(result);
console.log(...delta1.map(v => v.toString().padStart(2, ' ')));
console.log(...delta2.map(v => v.toString().padStart(2, ' ')));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!