1

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.

3
  • 1
    can 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) Commented Apr 17, 2015 at 3:57
  • Can u provide me code for that? Commented Apr 17, 2015 at 3:58
  • Please give sample input and output. Please format your code properly. Commented Apr 17, 2015 at 4:29

5 Answers 5

3

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));

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

6 Comments

I just want only remaining element that is remains in ar after deleting from both arrays
@BDhara So, you want only ar with duplicates from arr removed ?
yah only ar with remaining elements
Then you just need to do the reverse of what i did and don't concatenate :)
can u plz edit ur code? i have tried using 3 for loops and its working but may b it will take time
|
2

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);

3 Comments

I tried that it wont work bcz after splicing 1st element from arr index is also deleted that wont give appropriate answer
@BDhara, it does give the appropriate answer, check my edit.
thanx for ur reply but I want something different suppose in arr elemnts are 66,67,68,69,70 and in 65,66,67,68,69,70 then when both arrays matches then remove same element from both and remaining element of ar add into newID
2

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 ]

2 Comments

I dont want to use library just for comparison.. only javascript
yeah,i learn this library yesterday and just want to give you help :D
1

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. :)

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.