Open xszi opened 3 years ago
集合是由一组无序且唯一(即不能重复)的项组成的类似数组的数据结构。
手写实现一个集合:
function Set () { let items = {} // this.has = function (value) { // return value in items // } // 第二种更好的方法 this.has = function (value) { return items.hasOwnProperty(value) } this.add = function (value) { if (!items.has(value)) { items[value] = value return true } return false } this.remove = function (value) { if (items.has(value)) { delete items[value] return true } return false } this.clear = function () { items = {} } // 用在现代浏览器 this.size = function () { return Object.keys(items).length } // hasOwnProperty可以保证获取的属性为当前对象自有属性,而不是从原型继承过来的的额外属性,可以用在所有浏览器 this.sizeLegacy = function () { let count = 0 for (let key in items) { if (items.hasOwnProperty(key)) { ++count } } return count } this.values = function () { let values = [] for (let i = 0; i< Object.keys(values).length; i++) { values.push(items[keys[i]]) } return values } this.valuesLegacy = function () { let values = [] for (let key in items) { if (items.hasOwnProperty(key)) { values.push(items[key]) } } return values } // 交集 this.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 } // 差集 this.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 } // 判断子集 this.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 } } }
手写实现一个集合: