rex
rex

Reputation: 1025

Comparing two arrays and getting the non duplicate(not unique) values

I have two sets of arrays. I need to get the letters that are not in both arrays. First it should check if index 0 'a' is on both arrays. If it is in both than it should delete 'a' from both(just the first 'a' in first array not the one at the end(index 3). And go the the second item 'b' using same logic.

var arrayA = ['a','b','c','a'];

var arrayB = ['a','d','f','c'];

var arrayC = []; //Shoud have the result[b,a,d,f]

The code is set up at http://jsfiddle.net/rexonms/AbFYh/#base

HTML

 <p class="arrayA">Array A</p>
 <p class="arrayB">Array B</p>
 <p class="arrayC">Array C</p>

​ jQuery

var arrayA = ['a','b','c','a'];
var arrayB = ['a','d','f','c'];
var arrayC = []; //Shoud have the result[b,a,d,f]

$('.arrayA').text('ArrayA: ' + arrayA);
$('.arrayB').text('ArrayB: ' + arrayB);

$.each(arrayA, function(indexA,valueA) {
$.each(arrayB, function(indexB, valueB){

    if(valueA != valueB)
    {
        arrayC.splice(valueA);            
    }
});

$('.arrayC').text('ArrayC: ' + arrayC);
});

Upvotes: 2

Views: 9386

Answers (5)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

A vanilla JavaScript solution :

var sa = arrayA.join('');
var sb = arrayB.join('');
var sc = sa+sb;
for (var i=0; i<sc.length;i++) {
   var c = sc.charAt(i);
   if (sa.indexOf(c)==-1 || sb.indexOf(c)==-1) arrayC.push(c);
}

Using jQuery, you can also use inArray :

var sc = arrayA.join('')+arrayB.join('');
for (var i=0; i<sc.length;i++) {
   var c = sc.charAt(i);
   if ($.inArray(c, arrayA)==-1 || $.inArray(c, arrayB)==-1) arrayC.push(c);
}

The result, as was noted by Random, isn't ["b", "a", "d", "f"] but ["b", "d", "f"] with both solutions.

If you want to define "unicity" as "not present in the other array at the same index", here it is :

 for (var i=Math.min(arrayA.length, arrayB.length); i-->0;) {
   if (arrayA[i]!=arrayB[i]) {
      if ($.inArray(arrayA[i], arrayC)==-1) arrayC.push(arrayA[i]);
      if ($.inArray(arrayB[i], arrayC)==-1) arrayC.push(arrayB[i]);
   }
}   

EDIT : I've read the new definition of your edit. This doesn't seem to be related to unicity at all. I don't understand what you want. I give up but I left this answer (at least for now) as it answers the first question and may be useful.

Upvotes: 1

Jay
Jay

Reputation: 41

This was not documented, but it works..

<script type="text/javascript">
    var arr1=[1,2,3,4,5];
    var arr2=[3,4,5,7,8,9,6];
    $(document).ready(function(){
        var arr=$(arr1).not(arr2).get();
        alert(arr.length);
    });
</script>

Upvotes: 4

thecodeparadox
thecodeparadox

Reputation: 87073

var arrayA = ['a', 'b', 'c', 'a'];
var arrayB = ['a', 'd', 'f', 'c'];
var result = [];

function myArray( a, b ) {
   for( var i = 0; i < a.length; ++i ) {

        // find matched index of arrayB
        var bIndex = b.indexOf(a[i]);

        // if match found
        if( bIndex >= 0 ) {
            // remove element from arrayA
            a.splice( i, 1 );

            // remove element from arrayB of matched index
            b.splice( bIndex, 1 );
        }
    }

    // return result
    return a.concat(b);
}
result = myArray( arrayA, arrayB );

Working sample

Upvotes: 1

nbrooks
nbrooks

Reputation: 18233

Here is a working version of what you want: http://jsfiddle.net/Zzt5f/

var arrayA = ['a','b','c','a'];
var arrayB = ['a','d','f','c'];
var arrayC = []; //Should have the result[b,a,d,f]

$('.arrayA').text('ArrayA: ' + arrayA);
$('.arrayB').text('ArrayB: ' + arrayB);

$.each(arrayA, function(indexA,valueA) {
    $.each(arrayB, function(indexB, valueB){
        if(valueA == valueB)
        {
            arrayA[indexA]=null;
            arrayB[indexB]=null;
            return false; //break out of inner each loop                     
        }
    });
});

$.each(arrayA.concat(arrayB),function(idx,val) {
    if(val!=null) arrayC.push(val);
});

$('.arrayC').text('ArrayC: ' + arrayC);
alert(arrayC);

As you see I made only a few modifications to your original code. Firstly, since you are trying to remove the duplicate values, you need to check if valueA==valueB, not vice-versa. Once a match has been found, the second iteration needs to halt to prevent removal of a second duplicate in the second array, hence the return false.

I didn't use the array.splice method, because this actually creates and returns a new array by removing values from the array it is called on, so essentially it wasn't doing anything the way you were using it. It felt cleaner to not keep creating new arrays within the loop. Note that this method will actually modify arrayA and arrayB, so if you need them again later on you will have to clone them.

Upvotes: 2

Tats_innit
Tats_innit

Reputation: 34107

See bit different demo : http://jsfiddle.net/c8YLf/ or http://jsfiddle.net/S6FXs/5/

code

var a= ['a','b','c','a'];
var b= ['a','d','f','c'];

    var unique = $.grep(a, function(element) {
        return $.inArray(element, b) == -1;
    });

    var unique2 = $.grep(b, function(element) {
        return $.inArray(element, a) == -1;
    });
var result = unique + unique2;

alert(result); ​

or might help: JavaScript array difference

   var arrayA = ['a','b','c','a'];
var arrayB = ['a','d','f','c'];

function non_duplicate(arr1, arr2) {
      diff = [];
      joined = arr1.concat(arr2);
      for( i = 0; i <= joined.length; i++ ) {
        current = joined[i];
        if( joined.indexOf(current) == joined.lastIndexOf(current) ) {
          diff.push(current);
        }
      }
      return diff;
    }


var union = non_duplicate(arrayA,arrayB );
alert(union);
​

Upvotes: 0

Related Questions