Trying to solve symmetric difference using Javascript

后端 未结 16 1172
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 10:12

I am trying to figure out a solution for symmetric difference using javascript that accomplishes the following objectives:

  • accepts an unspecified number of ar
相关标签:
16条回答
  • 2020-11-29 10:57

    As with all problems, it's best to start off writing an algorithm:

    Concatenate versions of the arrays, where each array is filtered to contain those elements which no array other than the current one contains

    Then just write that down in JS:

    function sym() {
      var arrays = [].slice.apply(arguments);
    
      return [].concat.apply([],               // concatenate
        arrays.map(                            // versions of the arrays
          function(array, i) {                 // where each array
            return array.filter(               // is filtered to contain
              function(elt) {                  // those elements which
                return !arrays.some(           // no array
                  function(a, j) {             // 
                    return i !== j             // other than the current one
                      && a.indexOf(elt) >= 0   // contains
                    ;
                  }
                );
              }
            );
          }
        )
      );
    }
    

    Non-commented version, written more succinctly using ES6:

    function sym(...arrays) {
      return [].concat(arrays . 
        map((array, i) => array . 
          filter(elt => !arrays . 
            some((a, j) => i !== j && a.indexOf(elt) >= 0))));
    }
    
    0 讨论(0)
  • 2020-11-29 10:57

    // Set difference, a.k.a. relative compliment
    const diff = (a, b) => a.filter(v => !b.includes(v))
    
    const symDiff = (first, ...rest) => 
      rest.reduce(
        (acc, x) => [
          ...diff(acc, x), 
          ...diff(x, acc),
        ], 
        first,
      )    
    
    /* - - - */
    console.log(symDiff([1, 3], ['Saluton', 3]))    // [1, 'Saluton']
    console.log(symDiff([1, 3], [2, 3], [2, 8, 5])) // [1, 8, 5]

    0 讨论(0)
  • 2020-11-29 11:02

    function sym(args) {
      var initialArray = Array.prototype.slice.call(arguments);
      var combinedTotalArray = initialArray.reduce(symDiff);
    
      
      // Iterate each element in array,  find values not present in other array and push values in combinedDualArray if value is not there already
      // Repeat for the other array (change roles)
      function symDiff(arrayOne, arrayTwo){
        var combinedDualArray = [];
        arrayOne.forEach(function(el, i){
          if(!arrayTwo.includes(el) && !combinedDualArray.includes(el)){
            combinedDualArray.push(el);
          }
        });
          
        arrayTwo.forEach(function(el, i){
          if(!arrayOne.includes(el) && !combinedDualArray.includes(el)){
            combinedDualArray.push(el);
          }
        });
        combinedDualArray.sort();
        return combinedDualArray;
      }
      
      return combinedTotalArray;
    }
    
    console.log(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]));

    0 讨论(0)
  • 2020-11-29 11:02

    Create a Map with a count of all unique values (across arrays). Than concat all arrays, and filter non unique values using the Map.

    const symsym = (...args) => {
      // create a Map from the unique value of each array
      const m = args.reduce((r, a) => {
        // get unique values of array, and add to Map
        new Set(a).forEach((n) => r.set(n, (r.get(n) || 0) + 1));
        
        return r;
      }, new Map());
      
      // combine all arrays
      return [].concat(...args)
        // remove all items that appear more than once in the map
        .filter((n) => m.get(n) === 1); 
    };
    
    console.log(symsym([1, 1, 2, 6], [2, 3, 5], [2, 3, 4])); // => Desired answer: [1, 1, 6, 5, 4]

    0 讨论(0)
提交回复
热议问题