Examples of set deduplication, intersection, union, and difference functions implemented in JS

亚连
Release: 2018-05-30 15:55:10
Original
1575 people have browsed it

This article mainly introduces the collection deduplication, intersection, union, difference set functions implemented by JS, and analyzes related implementation techniques such as set deduplication, intersection, union, difference set and other related implementation techniques based on arrays implemented by javascript in the form of examples. , Friends who need it can refer to

The examples in this article describe the set deduplication, intersection, union, and difference set functions implemented by JS. Share it with everyone for your reference, the details are as follows:

1. js implements the set operation of the array

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);
Copy after login

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 ]
Copy after login

The above is what I compiled for everyone , I hope it will be helpful to everyone in the future.

Related articles:

jquery implements drag file upload loading progress bar function

Analysis of javascript prototype and prototype chain

Detailed tutorial on using Angular CLI to generate Angular 5 projects

The above is the detailed content of Examples of set deduplication, intersection, union, and difference functions implemented in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!