Home  >  Article  >  Web Front-end  >  JS implementation of set deduplication, intersection, union and difference function example sharing

JS implementation of set deduplication, intersection, union and difference function example sharing

小云云
小云云Original
2018-03-14 09:06:211423browse

This article mainly introduces the set deduplication, intersection, union, and difference functions implemented by JS. It analyzes the related implementation techniques of set deduplication, intersection, union, difference, etc. based on arrays implemented by JavaScript in the form of examples. , friends in need can refer to it, I hope it can help everyone.

1. js implements the set operation of arrays

In order to facilitate testing, we use nodejs here, the code is like set_operation.js

function array_remove_repeat(a) { // 去重
  var r = [];
  for(var i = 0; i < a.length; i ++) {
    var flag = true;
    var temp = a[i];
    for(var j = 0; j < r.length; j ++) {
      if(temp === r[j]) {
        flag = false;
        break;
      }
    }
    if(flag) {
      r.push(temp);
    }
  }
  return r;
}
function array_intersection(a, b) { // 交集
  var result = [];
  for(var i = 0; i < b.length; i ++) {
    var temp = b[i];
    for(var j = 0; j < a.length; j ++) {
      if(temp === a[j]) {
        result.push(temp);
        break;
      }
    }
  }
  return array_remove_repeat(result);
}
function array_union(a, b) { // 并集
  return array_remove_repeat(a.concat(b));
}
function array_difference(a, b) { // 差集 a - b
  //clone = a
  var clone = a.slice(0);
  for(var i = 0; i < b.length; i ++) {
    var temp = b[i];
    for(var j = 0; j < clone.length; j ++) {
      if(temp === clone[j]) {
        //remove clone[j]
        clone.splice(j,1);
      }
    }
  }
  return array_remove_repeat(clone);
}
a = [1,2,3,4,5];
b = [3,4,5,6,7];
c = array_intersection(a, b);
d = array_union(a, b);
e = array_difference(a, b);
f = array_difference(b, a);
console.log("test array a:", a, " b:", b);
console.log("a & b :", c);
console.log("a + b :", d);
console.log("a - b:", e);
console.log("b - a:", f);

2 . Test

We use nodejs here to test

Test results:

stephen@stephen:~/openstack/demo/nodejs$ node set_operation.js
test array a: [ 1, 2, 3, 4, 5 ]  b: [ 3, 4, 5, 6, 7 ]
a & b : [ 3, 4, 5 ]
a + b : [ 1, 2, 3, 4, 5, 6, 7 ]
a - b: [ 1, 2 ]
b - a: [ 6, 7 ]

Related recommendations:

js array deduplication Detailed explanation of sorting

Example detailed explanation of several ideas for deduplication of javascript arrays

Analysis of JS simple implementation of array deduplication

The above is the detailed content of JS implementation of set deduplication, intersection, union and difference function example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn