-2

UPDATE: This question is not a duplicate of suggested question, since I want to avoid duplicate calls to compare, since it is transitive.

I have two array of objects, say:

const A = [
  { id: 1, title: 'alice' },
  { id: 2, title: 'bob' },
  { id: 3, title: 'charlie' },
];

const B = [
  { id: 1, title: 'alice' },
  { id: 3, title: 'charlie' },
  { id: 4, title: 'dasha' },
  { id: 5, title: 'eric' },
];

An id identifies the object univocally (i.e.: title and all other props of an object with a specific id are the same for any object in array A and B).

To compare them I currently do:

A.forEach(a => {
  B.forEach(b => {
    compare(a, b);
  })
})

The compare() function is transitive, so compare(a, b) === compare (b, a).
How can I avoid comparing twice the same item?
For example, I want do avoid calling compare(a.1, b.3) and then compare(a.3, b.1) (where a.1 is item from array A with id 1).

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • 2
    Can you share more details? What makes you think that the given code compares anything twice? – Nico Haase Jan 25 '21 at 13:32
  • Even after your addition, I don't get your code. If you iterate over `A` in the outer loop, there's no way that the first argument to `compare` can be a value of `B` – Nico Haase Jan 25 '21 at 13:36
  • @Liam: no, sorry, I don't thinks so... – MarcoS Jan 25 '21 at 13:41
  • What do you want to achieve? – Hassan Imam Jan 25 '21 at 13:42
  • You probably want a modification of the [merge sort algorithm](https://en.wikipedia.org/wiki/Merge_sort). If you google this problem there are LOTS of solutions out there – Liam Jan 25 '21 at 13:43
  • @NicoHaase: I'd like to avoid calling `compare(a, b)` for a.id == 1 && b.id == 3 after I did already call `compare(a, b)` for a.id == 3 && b.id == 1 – MarcoS Jan 25 '21 at 13:43
  • Why do you want to avoid that? Please share more details by editing your question. In the given example. you would compare each item of A with all items of B only once – Nico Haase Jan 25 '21 at 13:46
  • @NicoHaase: sorry, I used a wrong example... Updated my question... For example, I want do avoid calling `compare(a.1, b.3)` and then `compare(a.3, b.1)` (where `a.1` is item from array `A` with id `1`). – MarcoS Jan 25 '21 at 13:52
  • Why do you want to avoid that? Is the comparison of that item itself so "expensive" that comparing the ID is sufficient? – Nico Haase Jan 25 '21 at 13:53
  • No... The arrays are huge, and I would like comparing the same id's twice, when I already know the answer, since the compare function is transitive... And - most important - I know that objects ith the same id contain the same data... – MarcoS Jan 25 '21 at 13:56
  • 1
    Then: what keeps you from storing such data in any kind of list? – Nico Haase Jan 25 '21 at 14:27
  • You're right... That's possibly the right approach... I'm not so confortable using lists (for example, JS `sets`?), unfortunately... I'll better study them in future... – MarcoS Jan 25 '21 at 14:34

1 Answers1

1

To make avoid comparision you have to skip than by condition

a.id < b.id

this mean that you will never compare a.3 b.3

but if you will need compare a.3 b.3

use a.id <= b.id

const A = [
  { id: 1, title: 'alice' },
  { id: 2, title: 'bob' },
  { id: 3, title: 'charlie' },
];

const B = [
  { id: 5, title: 'eric' },
  { id: 1, title: 'alice' },
  { id: 3, title: 'charlie' },
  { id: 4, title: 'dasha' }
  
];


A.forEach(a => {
  B.forEach(b => {
   if (a.id <= b.id) compare(a, b);
  })
})

function compare(a, b){
 console.log(`compare(${a.id}, ${b.id})`);
}

I think you need something like this

A.forEach(a => {
  B.forEach(b => {
    if (a.id <= b.id)
       compare(a, b);
  })
})

to skip (a.3, b.3)

Updated in case you are not able to compare ID generate hash according answer. So you will be able to compare by the hash Code

Aziz Umarov
  • 296
  • 2
  • 8