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

179
Views
Is there a computationally efficient way of check if there exists difference(s) between two nested arrays?

I have two 2D arrays of ints and are the same length, but they are very large. I want to find if there exists at least one difference between the two arrays.

Note: I don't need to find out what the differences are, I just need to return true if there is at least one difference else false.

Right now I'm using two for loops to iterate through the indices and check if arr1[i][j] !== arr2[i][j], but this is taking over 60 seconds worst case due to size.

Is there a better way to make this comparison?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As long as you maintain the data structures in question (nested arrays), you can't get any faster than this, you have to check every element in general.

A few particular optimizations that you may conditionally apply however:

  1. I'm fairly certain that flat arrays are faster to index, and you can use i * width + j to maintain your accesors similar, though obviously not identical.

  2. If one of the arrays doesn't change often, and is so a reference you're checking against, you'll likely get very good results by hashing. You store the hash of your reference array every time you change it (again, it has to not be often), and every time you need to run your check against a new array you can hash he tested array and check it against the other calculated hash. Note that this can give false positives, so if the hashes match you need to actually check every element to make sure it actually is an equality.

And note that the above aren't either-or, implementing both will give you very good results!

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!