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

113
Views
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 answers
Answer question

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 Report

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 Report

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 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!