javaScript數(shù)據(jù)結(jié)構(gòu)--集合

集合是由一組無(wú)序且唯一的項(xiàng)組成的:

集合可以進(jìn)行 并集、交集、差集、子集操作。

集合的代碼實(shí)現(xiàn):

function Set() {

? ? var items = {};

? ? this.add = function(value) {

? ? ? ? if(!this.has(value)) {

? ? ? ? ? ? items[value] = value;

? ? ? ? ? ? return true;

? ? ? ? }else {

? ? ? ? ? ? return false;

? ? ? ? }

? ? }

? ? this.remove = function(value) {

? ? ? ? if(this.has(value)) {

? ? ? ? ? ? delete items[value];

? ? ? ? ? ? return true;

? ? ? ? }else {

? ? ? ? ? ? return false;

? ? ? ? }

? ? }

? ? this.has = function(value) {

? ? ? ? return value in items;

? ? }

? ? this.clear = function() {

? ? ? ? this.items = {};

? ? }

? ? this.size = function() {

? ? ? ? return Object.keys(items).length;

? ? }

? ? this.values = function() {

? ? ? ? var keys = [];

? ? ? ? for(let key in items) {

? ? ? ? ? ? keys.push(key);

? ? ? ? }

? ? ? ? return keys;

? ? }

? ? this.union = function(otherSet) {

? ? ? ? var unionSet = new Set();

? ? ? ? let values = this.values();

? ? ? ? for(let i=0;i<values.length;i++) {

? ? ? ? ? ? unionSet.add(values[i]);

? ? ? ? }

? ? ? ? values = otherSet.values();

? ? ? ? for(let i=0; i< values.length; i++) {

? ? ? ? ? ? unionSet.add(values[i]);

? ? ? ? }

? ? ? ? return unionSet;

? ? }

? ? this.intersection = function(otherSet) {

? ? ? ? var intersection = new Set();

? ? ? ? let values = this.values();

? ? ? ? for(let i=0; i< values.length; i++) {

? ? ? ? ? ? if(otherSet.has(values[i])) {

? ? ? ? ? ? ? ? intersection.add(values[i]);

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return intersection;

? ? }

? ? this.difference = function(otherSet) {

? ? ? ? const difference = new Set();


? ? ? ? let values = this.values();

? ? ? ? for(let i=0; i< values.length; i++) {

? ? ? ? ? ? if(!otherSet.has(values[i])) {

? ? ? ? ? ? ? ? difference.add(values[i]);

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return difference;

? ? }

? ? this.subset = function(otherSet) {

? ? ? ? if(this.size() > otherSet.size()) {

? ? ? ? ? ? return false;

? ? ? ? }else{

? ? ? ? ? ? const values = this.values();

? ? ? ? ? ? for(let i=0; i< values.length; i++) {

? ? ? ? ? ? ? ? if(!otherSet.has(values[i])) {

? ? ? ? ? ? ? ? ? ? return false;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? return true;

? ? ? ? }

? ? }

}

const set = new Set();

set.add("aa");

set.add("aa");

set.add("bb");

set.add("ee");

console.log(set.values()); // ['aa', 'bb',? 'ee']

console.log(set.size()); // 2

console.log(set.remove('dd')); // false

set.remove("aa");

console.log(set.values()); // ['bb, 'ee']

const set1 = new Set();

set1.add("bb");

set1.add("ff");

set1.add("gg");

console.log(set.union(set1).values()); // [ 'bb', 'ee', 'ff', 'gg' ]

console.log(set.intersection(set1).values()); // [ 'bb' ]

console.log(set.difference(set1).values()); // ['ee']

console.log(set.subset(set1)); // false

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容