I want a function, subtract, that will take two arrays, and return all of the elements in array 1 that are not in array 2.
what is the fastest that this can be implemented in js? Is it o(n)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Another option, faster O(n) time, but double memory (still linear), is to create your own hashmap implementation.
Create a hash function. Do a loop through one array and hash all elements. Store (hash, object) pair in another array, call it hash array. Now loop through array 2, and hash each element. Let the hash be the position in hash array, so you can see if you have a collision. If you have a collision check if the object in hash array is the same as the object in current array (that you’re looping over).