I have two arrays, named arr and ar. Suppose ar has 7 element and arr has 6 elements. I want to remove an element if it is the same in both, otherwise assign it to a new variable. I have this so far:
var arr = new Array(); // Elements are 65,66,67,68,69,70
var newID = new Array();
var ar = new Array(); // 64,65,66,67,68,69,70
if (ar.length != arr.length) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < ar.length; j++) {
if (arr[i] == ar[j]) {
delete ar[i];
arr.splice(i, 1);
break;
}
newID = ar[i];
}
}
for (var i = 0; i < ar.length; i++) {
newID = ar[i];
}
This does not work properly as it will compare with an undefinded value. Please help me correct it.
-
1can u use a single while loop with two pointers : while(i< array1.length && j < array2.length ) instead of two for loops. that will be O(n) instead of O(n^2)Ace McCloud– Ace McCloud2015年04月17日 03:57:15 +00:00Commented Apr 17, 2015 at 3:57
-
Can u provide me code for that?user4729539– user47295392015年04月17日 03:58:13 +00:00Commented Apr 17, 2015 at 3:58
-
Please give sample input and output. Please format your code properly.user663031– user6630312015年04月17日 04:29:13 +00:00Commented Apr 17, 2015 at 4:29
5 Answers 5
Here is one more using reduce
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [1, 3, 5, 7, 9];
var result = arr1.reduce(function (prev, value) {
var isDuplicate = false;
for (var i = 0; i < arr2.length; i++) {
if (value == arr2[i]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
prev.push(value);
}
return prev;
}, []);
alert(JSON.stringify(result.concat(arr2)));
EDITED
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [1, 3, 5, 7, 9];
arr2 = arr2.reduce(function (prev, value) {
var isDuplicate = false;
for (var i = 0; i < arr1.length; i++) {
if (value == arr1[i]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
prev.push(value);
}
return prev;
}, []);
alert(JSON.stringify(arr2));
answered Apr 17, 2015 at 4:43
Vigneswaran Marimuthu
2,54216 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Vigneswaran Marimuthu
@BDhara So, you want only
ar with duplicates from arr removed ?Vigneswaran Marimuthu
Then you just need to do the reverse of what i did and don't concatenate :)
|
You can try the following:
var arr = [1, 2, 3, 4, 5, 6, 7];
var ar = [2, 4, 6, 8, 10];
var newID = [];
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < ar.length; j++){
if(arr[i] == ar[j]){
newID.push(arr[i]);
arr.splice(i, 1);
ar.splice(j, 1);
break;
}
}
}
alert(arr);
alert(ar);
alert(newID);
answered Apr 17, 2015 at 4:02
A.J. Uppal
19.3k7 gold badges48 silver badges82 bronze badges
3 Comments
A.J. Uppal
@BDhara, it does give the appropriate answer, check my edit.
i just want to add a js library -- lodash
var _ = require('lodash');
var array1 = [1,2,3,4,5];
var array2 = [3,1,5];
_.difference(array1,array2)
// returns [ 2, 4 ]
answered Apr 17, 2015 at 4:25
qianjiahao
3991 gold badge3 silver badges10 bronze badges
2 Comments
qianjiahao
yeah,i learn this library yesterday and just want to give you help :D
I'll give you the following solutions.
var ar = [1,2,3,4,5,6,7];
var arr = [3,6,7,8,9,2];
//ES5
var mergedArray = ar.concat(arr);
var newId = [];
for(var i=0;i<mergedArray.length;i++){
var id = mergedArray[i];
if(newId.indexOf(id) !== -1) continue;
newId.push(id);
}
//or smartter
var newId = ar.concat(arr).filter(function(id, pos, self) {
return self.indexOf(id) === pos;
});
//or ES6
var mergedArray = ar.concat(arr);
var newId = [];
for(let id of mergedArray){
if(newId.indexOf(id) !== -1) continue;
newId.push(id);
}
//or smarter ES6
var newId = ar.concat(arr).filter((id, pos, self) => self.indexOf(id) === pos);
The choice is yours. :)
answered Apr 17, 2015 at 4:35
Lewis
15k14 gold badges70 silver badges88 bronze badges
Comments
var arr = [1, 2, 2, 3, 4, 5, 6, 7];
var ar = [2, 4, 6, 8, 10];
var combinearray = [...arr, ...ar]
var newArr = new Set(combinearray);
console.log(...newArr)
mplungjan
180k29 gold badges183 silver badges246 bronze badges
answered Sep 30, 2019 at 11:59
Mehul Patel
211 gold badge1 silver badge3 bronze badges
Comments
lang-js