8

I have 2 arrays that they are identical at first but the user may remove (not able to add but just remove) items from the second array. I want to find items that are in the first array but not in the second one.

I can think of several ways to do this, but as these arrays can be very large, I'm curious to find out if anyone can offer a more efficient way:

$.grep( firstArray, function( n, i ){
  return $.inArray(n, secondArray) == -1;
});
2
  • Please share those two arrays Commented Mar 4, 2018 at 10:15
  • If it works I think your way is quite efficient Commented Mar 4, 2018 at 10:17

5 Answers 5

22

You could try to make use of filter and indexOf array methods as below:

var firstArray = [1,2,3,4,5,6];
var secondArray = [3,4,6];

var result = firstArray.filter(item => secondArray.indexOf(item) == -1);

console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

I don't get why people downvote correct answers. If you have no clue, simply shut up and learn. This is the correct answer.
As efficiency was explicitly mentioned by the author: Assuming sorted arrays one might want to do a binary search
7

Assuming the arrays have the same order, then you could filter with an index as closure.

var array1 = [1, 2, 3, 4, 5, 6, 7, 1, 2, 3],
    array2 = [2, 4, 6, 7, 2],
    missing = array1.filter((i => a => a !== array2[i] || !++i)(0));
    
console.log(missing);

13 Comments

This is imposing additional restrictions and also far less readable than Christos' answer.
@connexo, the mentiond answer relies on unique values, which may not be given.
@NinaScholz great answer, Performance Comparison
@NinaScholz , could you please explain this part , a => a !== array2[i] || !++i)(0) ,thanks
@BrandonMcConnell, i need i for array2. if i would be the index of array1, it does not work to filter without an additional offset.
|
2

Do a .filter on the array and check if the array 2 does not have the element using .includes

var a1 = [1,2,3,4,5,6];
var a2 = [1,3,5];

var absent = a1.filter(e=>!a2.includes(e));

console.log(absent);

2 Comments

Thanks. let me check the performance time of these algorithms. because actually, I know a way to do it but looking for better performance.
@AshkanMobayenKhiabani yea but according this is a small and fast enough code to make this task done. .includes is better than .indexOf performance wise if you just need to check array contains an element or not.
1
function arr_diff (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
    a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
    if (a[a2[i]]) {
        delete a[a2[i]];
    } else {
        a[a2[i]] = true;
    }
}
for (var k in a) {
    diff.push(k);
}
return diff;

}

Use this function to get difference between two sets.
A:Set 1
B:Set 2
A-B:Elements that are present in A but, not in B
Basic set theory

1 Comment

your answer has great performance, but as I can only mark one answer, I chose Nina's as its shorter. +1
0

As user can delete items. So you can add those deleted items in a different array.

var deltedItems = [];
var position = 0;

function onDeleteItem(value){
   deltedItems[position] = value;
   position++;
}

Here, you can find all your deleted items in deltedItems variable. By using this logic you can eliminate search cost of your program.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.