0

I have been writing the code to make a symmetric difference between to or more arrays. As you know, the symmetric difference is about excluding the elements which are in both sets of datas. More info: https://en.wikipedia.org/wiki/Symmetric_difference

This is my code:

  //This function drops any repeated element from a vector
    function unify(arr){
        var result=arr.reduce(function(vector,num,index,self){
         var len=self.filter(function (val){
           return val===num;
         }).length;
        if (len>1){
          var pos=self.indexOf(num);
          self.splice(pos,1);
        }
        vector=self;
        return vector;
        },[]);
      
      return result;
    }
    
    function sym(args) {
      var arg = Array.prototype.slice.call(arguments); 
      var compact= arg.map(function(vector){
        return unify(vector);
      });
      
      //We compare the vectors and delete any repeated element before we concat them
      return compact.reduce(function(prev,next,index,self){
        for (var key in next) {
          var entry=next[key];
          var pos=prev.indexOf(entry);
          if (pos!==-1){
            prev.splice(pos,1);
            next.splice(key,1);
          }
            
        }
        return prev.concat(next);
      });
      
    }
    
    console.log(sym([1, 2, 3], [5, 2, 1, 4]));

I don't understand what am I doing wrong. I expected to get a result of [3,4,5] but that is not the result I get.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
myhappycoding
  • 648
  • 7
  • 20
  • Do we really need to watch a youtube video to understand your question? Also, we really dont know what you're doing wrong, because you've not described a problem! – Jamiec Jul 21 '16 at 08:19
  • sorry to upset you, I didn't know what a symmetric difference was until I saw the video. On the other hand, If you execute the code you will see that it doesn't do what I am describing (symmetric difference). What do you need me to describe? – myhappycoding Jul 21 '16 at 08:24
  • What *does* it do, what *should* it do. Create a [mcve] to demonstrate. – Jamiec Jul 21 '16 at 08:26
  • you dont need to watch anything, do you? – myhappycoding Jul 21 '16 at 08:27
  • it needs to do a symmetric difference between arrays, and as I have said exclude the repeated numbers between both sets and have as a result the rest of numbers – myhappycoding Jul 21 '16 at 08:28
  • 1
    Ive updated your question - it now has a runnable example, and a description of what is wrong. – Jamiec Jul 21 '16 at 08:29

1 Answers1

1

When you do the splice, the indexing of the array is changed. So it would skip some values.

In your example, prev = [1,2,3] and next = [5,2,1,4].

After the first splice @ key = 1 both arrays look like this prev = [1,3] and next = [5,1,4]. The next key is 2 which skips the entry 1 in next. Because of the splice, this index has shifted one to the left.

My solutions (sorry, I'm on my phone else I'd have written code)

  1. Write a more deliberate loop, and increment key only when necessary. So if there's a splice, don't increment, because the would-be next entry has been shifted to the current key.
  2. If you must keep the for a in b-type loop, consider having a duplicate but constant reference for the next array
Igbanam
  • 5,904
  • 5
  • 44
  • 68