Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

70
Vistas
How can I get a sequence of two array items in an array?

i am looking for a way to get a sequence of two array items in an array, especially an array of length 4

for example,

[[1,0], [2,3], [5,4], [0,0], [3,2], [1,4], [0,5]]

... should return :

[[3,2], [2,3], [1,4], [0,5]]

3 --^ 2 -----^ 1 ----^ 0 -----^ so [3, 2, 1, 0] for x

[[3,2], [2,3], [1,4], [0,5]]

2 -----^ 3 -----^ 4 ----^ 5 -----^ so [2, 3, 4, 5] for y

[[x1, y1], [x2, y2], [x3, y3], [x4, y3]]
// +1 or -1 for the first index
// and +1 or -1 for the second index
[[3,2], [2,3], [1,4], [0,5]] // is a sequence
[[0,0], [1,1], [2,2], [3,3]] // is a sequence
[[4,4], [3,3], [2,2], [1,1]] // is a sequence
[[4,3], [3,3], [2,2], [1,1]] // is not a sequence
[[1,2], [2,3], [4,5], [5,6]] // is a sequence

i tried to use for loops but it's illegible and confusing, maybe too difficult and this but it's only counting the longest sequence, not returning it :

const Z = x.sort((a, b) => a - b).reduce((count, val, i) => {
  return count += val + 1 === x[i + 1] ? 1 : 0
}, 1);
const Z2 = y.sort((a, b) => a - b).reduce((count, val, i) => {
  return count += val + 1 === y[i + 1] ? 1 : 0
}, 1);
      
console.log(Z, Z2) // 4 4
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could add the points to an object and check the order of four by using a factor of adding an offset for x and y.

If needed, you could add the check for horizontal or vertical points, too.

const
    four = array => {
        const
            data = array.reduce((r, [x, y]) => ((r[x] ??= {})[y] = true, r), {}),
            check = ([x, y], i, j) => {
                const temp = [];
                for (let k = 0; k < 4; k++, x += i, y += j) {
                    if (data[x]?.[y]) temp.push([x, y]);
                    else break;
                }
                if (temp.length === 4) return temp;
            };
            
        let result;
        array.some(p => result = check(p, 1, 1) || check(p, -1, 1));
        return result;
    },
    data = [[1, 0], [2, 3], [5, 4], [0, 0], [3, 2], [1, 4], [0, 5]];

console.log(four(data)); // [[3, 2], [2, 3], [1, 4], [0, 5]]
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda