2

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);
});
rex
  • 985
  • 5
  • 28
  • 48
  • 2
    on what basic b,a,d,f are written becuase a comes in both while other are unique –  Jul 07 '12 at 13:16
  • cause that how it should loop. First it will test the first letter 'a'. since 'a' is in both array it should delete from both and go to the second one in the second one 'b'. Since 'b' is not in the second array we leave 'b' as is. Then go to 'c', since 'c' is in both arrays it should be deleted from both. – rex Jul 07 '12 at 13:27

5 Answers5

4

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>
Jay
  • 41
  • 2
2

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.

nbrooks
  • 18,126
  • 5
  • 54
  • 66
1

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.

fragilewindows
  • 1,394
  • 1
  • 15
  • 26
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thanks for all the solutions. It gives me more insight of array. What I am trying to created is somewhat like http://www.wikihow.com/Play-%22Flame%22 – rex Jul 07 '12 at 15:24
1
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

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
0

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);
​
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77