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

112
Vistas
Compare two arrays of objects in Javascript

I have a list of co-ordinates of squares of a grid: Grid

Each square is defined by a class:

class Square {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

This is how the app works: First there is a list of randomly generated squares shown, and then the user has to click the squares from memory. (Not necessarily in order)

An example of how the question/answer array would be: (just an example, they are actually randomly generated)

const arr = [new Square(1, 1), new Square(2, 1)]

now, whenever the user clicks on a box, it goes into another array:

var selectedBlocks = [new Square(2, 1), new Square(1, 1)]

In this case, since the squares selected are equal, the function should return true.

What I've tried - I can't manage to get it without a double-for loop O(n^2). Is it possible to optimise for atleast O(n)?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use Array.some to find is item already selected or not

class Square {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

const arr = [new Square(1, 1), new Square(2, 1)];

const selectedBlocks = [new Square(2, 1), new Square(1, 1)];

const isSelected = (block) => {
  return selectedBlocks.some(
    (item) => item.x === block.x && item.y === block.y
  );
};

console.log(isSelected(new Square(2, 1)));
console.log(isSelected(new Square(2, 2)));
console.log(isSelected(new Square(1, 1)));

about 4 years ago · Juan Pablo Isaza Denunciar

0

I forgot that there were few built in methods and i wrote this i guess (nlogn) Time complexity code

class Square {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    pri(){
        return this.x + this.y;
    }
}

let inp = [new Square(2,1), new Square(2,3), new Square(1,3)]
let opt = [new Square(1,3), new Square(2,1), new Square(2,3)]

inp.sort((a, b) => {
    return a.pri() - b.pri();
});
opt.sort((a, b) => {
    return a.pri() - b.pri();
});
// assuminng they are of same len
let flag = true
for(i=0; i<inp.length ; i++){
    if(inp[i].x != opt[i].x || inp[i].y != opt[i].y){
        flag = false
        break
    }
}

console.log(flag)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Quoting from the answer on another question, the answers to that one may also help. If you just need to know whether A and B has same entries, simply use

JSON.stringify(A.concat().sort()) === JSON.stringify(B.concat().sort())

Link to the original answer is here.

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