js模拟实现一个Set集合

本文详细介绍了一种集合数据结构的实现方法,包括添加、删除、查询等基本操作,并深入探讨了集合之间的并集、交集、差集和子集运算。通过具体示例展示了集合操作在实际应用中的效率和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

集合是由一组无序且唯一(即不能重复)的项组成的。

  • add(value):向集合添加一个新的项。

  • delete(value):从集合移除一个值。

  • has(value):如果值在集合中,返回true,否则返回false。

  • clear():移除集合中的所有项。

  • size():返回集合所包含元素的数量。与数组的length属性类似。

  • values():返回一个包含集合中所有值的数组。


并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合。

交集:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合。

差集:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。

子集:验证一个给定集合是否是另一集合的子集。



class Set {
	constructor() {
		this.items = {};
	}

	has(value) {
		return this.items.hasOwnProperty(value);
	}

	add(value) {
		if (!this.has(value)) {
			this.items[value] = value;
			return true;
		} else {
			return false;
		}
	}

	remove(value) {
		if (this.has(value)) {
			delete this.items[value];
			return true;
		} else {
			return false;
		}
	}

	clear() {
		this.items = {};
	}

	size() {
		let count = 0;
		for (let key in this.items) {
			if (this.items.hasOwnProperty(key)) {
				++count;
			}
		}
		return count;
	}

	keys() {
		let keys = [];
		for (let key in this.items) {
			if (this.items.hasOwnProperty(key)) {
				keys.push(key);
			}
		}
		return keys;
	}

	values() {
		let values = [];
		for (let value in this.items) {
			if (this.items.hasOwnProperty(value)) {
				values.push(this.items[value]);
			}
		}
		return values;
	}

}
// 并集 A U B
Set.prototype.union = function (otherSet) {
	let 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;
}

// 交集 A ∩ B
Set.prototype.intersection = function (otherSet) {
	let intersectionSet = new Set();
	let values = this.values();

	for (let i = 0; i < values.length; i++) {
		if (otherSet.has(values[i])) {
			intersectionSet.add(values[i]);
		}
	}
	return intersectionSet;
}

// 差集 A-B
Set.prototype.difference = function (otherSet) {
	let differenceSet = new Set();
	let values = this.values();

	for (let i = 0; i < values.length; i++) {
		if (!otherSet.has(values[i])) {
			differenceSet.add(values[i]);
		}
	}
	return differenceSet;
}

// 子集 A⊆B
Set.prototype.subset = function (otherSet) {
	if (this.size() > otherSet.size()) {
		return false;
	} else {
		let values = this.values();
		for (let i = 0; i < values.length; i++) {
			if (!otherSet.has(values[i])) {
				return false;
			}
		}
		return true;
	}
}

let set = new Set();
set.add(1);
set.add(2);
// set.add(5);
// set.add(9);
console.log(set); // Set { items: { '1': 1, '2': 2 } }
console.log(set.keys()); // [ '1', '2' ]
console.log(set.values()); // [ 1, 2 ]
console.log(set.size()); // 2
set.remove(1);
console.log(set); // Set { items: { '2': 2 } }
let otherset = new Set();
otherset.add(1);
otherset.add(2);
otherset.add(3);
otherset.add(4);
console.log(set.union(otherset)); // Set { items: { '1': 1, '2': 2, '3': 3, '4': 4 } }
console.log(set.intersection(otherset)); //Set { items: { '2': 2 } }
console.log(set.difference(otherset)); // Set { items: {} }
console.log(set.subset(otherset)); // true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值