class Set {
constructor() {
this.items = {};
}
}
複製代碼
1. has() 檢驗某個元素是否存在於集合中javascript
has(element) {
return Object.prototype.hasOwnProperty.call(this.items, element); // 返回一個代表對象是否具備特定屬性的布爾值
}
複製代碼
2. add() 向集合添加一個新元素java
add(element) {
if (!this.has(element)) { // 檢查
this.items[element] = element; // 若是不存在,就把element添加到集合中,返回true {element:element}
return true;
}
return false; // 若是集合中有相同的元素,返回false便可
}
複製代碼
3. delete()從集合中刪除一個元素算法
delete(element) {
if (this.has(element)) { // 檢查,只能刪除存在與集合中的,(集合中都沒有,你刪誰去~)
delete this.items[element]; // 從集合中移除element
return true; // 返回true
}
return false; // 若是集合中沒有,返回false
}
複製代碼
4. size()返回集合中有多少元素數組
size() { // 返回集合中有多少元素
return Object.keys(this.items).length; // 使用原生的內置方法,把對象的key轉化爲數組,再返回其length
}
複製代碼
5. values()返回一個包含集合中全部全部值的數組瀏覽器
values() { // 返回一個包含集合中全部全部值的數組
return Object.values(this.items); // 一樣使用原生方法(它是在ECMAscript2017中被添加進來的,目前只在如今瀏覽器中可用)
}
複製代碼
1.並集 數據結構
union(otherSet) {
const unionSet = new Set(); // 建立一個新的集合
this.values().forEach(value => unionSet.add(value)); // 獲取第一個集合(當前的set類實例)全部的值並添加到新集合中
otherSet.values().forEach(value => unionSet.add(value)); // 獲取第二個集合(傳入的set類實例)全部的值並添加到新集合中
return unionSet; // 最後返回建立的新集合
}
複製代碼
2.交集 工具
intersection(otherSet) {
const intersectionSet = new Set(); // 建立一個新的集合
const values = this.values(); // 獲取第一個集合(當前的set類實例)
const otherValues = otherSet.values(); // 獲取第二個集合(傳入的set類實例)
let biggerSet = values; // 假設當前集合的元素較多
let smallerSet = otherValues; // 傳入集合的元素較少
if (otherValues.length - values.length > 0) { // 比較兩個集合的元素個數
biggerSet = otherValues; // 若是傳入集合的元素個數比當前集合的元素個數多的話,就交換較多的等於傳入的
smallerSet = values; // 較少的等於當前集合
}
smallerSet.forEach(value => { // 最後迭代較少集合
if (biggerSet.includes(value)) { // 若是較大的集合中也有這個元素
intersectionSet.add(value); // 添加到新集合當中
}
});
return intersectionSet; // 返回新集合
}
複製代碼
3. 差集學習
difference(otherSet) {
const differenceSet = new Set(); // 建立一個新的集合
this.values().forEach(value => { // 當前集合的值轉換爲數組,並循環
if (!otherSet.has(value)) { // 若是傳入的集合中沒有這個元素
differenceSet.add(value); // 把它添加到新集合中
}
});
return differenceSet; // 返回新的集合
}
複製代碼
4. 子集優化
isSubsetOf(otherSet) {
if (this.size() > otherSet.size()) { // 若是當前集合的元素比傳入集合多,那它確定不是傳入集合的子集,返回false
return false;
}
let isSubset = true; // 先假設當前集合是傳入集合的子集
// 迭代當前集合,當發現一個返回false,便再也不執行。
this.values().every(value => {
if (!otherSet.has(value)) { // 驗證迭代的元素是否也存在傳入集合中
isSubset = false; // 只要有一個不是就改變變量
return false; // 返回false 再也不往下執行
}
return true; // 若是都是,返回true
});
return isSubset; // 最後返回變量isSubset
}
複製代碼
const set = new Set();
set.add(1);
console.log(set.values()); // 輸出@Iterator
console.log(set.has(1)); // 輸出true
console.log(set.values()); // 輸出1
複製代碼
ES6Set運算模擬ui
const setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);
const setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);
複製代碼
const union = (set1, set2) => { // 普通模擬
const unionAb = new Set();
set1.forEach(value => unionAb.add(value));
set2.forEach(value => unionAb.add(value));
return unionAb;
};
console.log(union(setA, setB)); // [1,2,3,4]
console.log(new Set([...setA, ...setB])); // 使用 擴展運算符模擬 [1,2,3,4]
複製代碼
const intersection = (set1, set2) => { // 普通模擬, 未經優化的
const intersectionSet = new Set();
set1.forEach(value => {
if (set2.has(value)) {
intersectionSet.add(value);
}
});
return intersectionSet;
};
console.log(intersection(setA, setB)); // [2,3]
console.log(new Set([...setA].filter(x => setB.has(x)))); // 使用 擴展運算符模擬 [2,3]
複製代碼
const difference = (set1, set2) => { // 普通模擬
const differenceSet = new Set();
set1.forEach(value => {
if (!set2.has(value)) {
differenceSet.add(value);
}
});
return differenceSet;
};
console.log(difference(setA, setB));// [1]
console.log(new Set([...setA].filter(x => !setB.has(x)))); // 使用 擴展運算符模擬 [1]
複製代碼
好了,今天的隨手筆記完事兒了。本文內容全來自本人閱讀過《學習Javascript數據結構與算法》第七章後稍加整理而成。