-1

It's an interview task that says: Lets imagine, you receive from the backend API 2 very large arrays with names. Your task is to check if values from array t are present in y. Please, explain your approach/es.

var t = ['toy1', 'toy2', 'toy3', ..., A];

var y = ['toy3', 'toy1'];


I want the best method and fastest to compare them, to return true or false or to return index.(whatever) assuming,that they are too large arrays.

Because we don't know if all the array elements are strings or objects(in var t). What is the best aproach?

The best aproach is if all array values are strings to do t.some( name => y.includes(name) );

Fdust
  • 11
  • 1
  • What could the items inside these arrays be? – SSM May 26 '22 at 20:08
  • *"return true or false or to return index"*: so you want to call a function, passing 2 large arrays, and that function returns false, true or an index? What would that mean? For instance, it would return index 15. What does that mean? More importantly, what is the problem you have in writing code for this? – trincot May 26 '22 at 20:09
  • It's an interview task that says:Lets imagine, you receive from the backend API 2 very large arrays with names. Your task is to check if values from array t are present in y. Please, explain your approach/es. – Fdust May 26 '22 at 20:22
  • @PeterSeliger yes,but what could be my approach to this task...What would propably work to resolve this. – Fdust May 26 '22 at 23:16
  • Doesn't the OP want to check if either [`every`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) element or just [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) elements of one array is/are present in the other array. Or other way around, whether the 2nd array contains/[`includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) `every`/`some` element/s of the first array? – Peter Seliger May 27 '22 at 07:44
  • And following my gut feeling I would iterate the shorter/smaller array by either `every` or `some` and use the nested `includes` on the longer/larger array. And for the real big data one could think about optimizing the *includes* part by a `Map` or object based lookup approach. – Peter Seliger May 27 '22 at 07:54
  • Ahh, I see, the OP meanwhile did come up with own thoughts and the correct solution (upvote) for string values. In order to solve the problem of mixed unknown types one needs to write an own `isEqual` method which computes whether two types are equal by just value (primitives) or by object structure and values (array/object based data-structures). Such an `isEqual` function needs to be used as part of the [`find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) method which has to replace the `includes` method (which is good enough for primitives). – Peter Seliger May 27 '22 at 08:08
  • Helpful might be the `isDeepDataStructureEquality` implementation of an answer to following question ... [_"How to get the intersection of two sets while recognizing equal set values/items not only by reference but by their equal structures and entries too?"_](https://stackoverflow.com/questions/71015428/how-to-get-the-intersection-of-two-sets-while-recognizing-equal-set-values-items/71016510#71016510) – Peter Seliger May 27 '22 at 08:20
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 28 '22 at 02:48

0 Answers0