1、集合的组成
集合元素可以由不同类型的数据组成,但是元素不能重复
2、集合封装
class MySet {
constructor() {
this.item = {};
}
add(element) {
//元素不能重复
if (!this.hasOwn(element)) {
this.item[element] = element;
return true;
}
return false;
}
delete(element) {
if (this.hasOwn(element)) {
return delete this.item[element];
}
return false;
}
clear() {
this.item = {};
return true;
}
size() {
return this.setValues().length;
}
setValues() {
return Object.keys(this.item);
}
hasOwn(element) {
//判断集合中是否已经存在element元素
//obj.hasOwnProperty()
// console.log({});
return this.item.hasOwnProperty(element);
}
//并集
static getUnionSet(set1, set2) {
let set = new MySet();
set1.setValues().forEach(element => {
set.add(element);
});
set2.setValues().forEach(element => {
set.add(element);
})
return set;
}
//交集
static differenceSet(set1, set2) {
let set = new MySet();
set1.setValues().forEach(element => {
if (!set2.hasOwn(element)) {
set.add(element);
}
});
return set;
}
//差集
static intersectionSet(set1, set2) {
let set = new MySet();
set1.setValues().forEach(element => {
if (set2.hasOwn(element)) {
set.add(element);
}
});
return set;
}
//子集
static subSet(set1, set2){
let result = true;
let set = new MySet();
set2.setValues().forEach(element => {
if(!set1.hasOwn(element)){
result = false;
}
})
return result;
}
}
1、字典的结构
字典元素是由键值对组成,键不能重复,但是值可以重复,相当于对象
2、字典封装
class Dictionary{
constructor(){
this.item = {};
}
set(key, value){
this.item[key] = value;
}
remove(key){
if(this.hasOwn(key)){
delete this.item[key];
return true;
}
return false;
}
hasOwn(key){
return this.item.hasOwnProperty(key);
}
get(key){
return this.item[key];
}
clear(){
this.item = {};
}
size(){
return this.keys().length;
}
//获取键
keys(){
return Object.keys(this.item);
}
//获取值
values(){
return Object.values(this.item);
}
}
数组准备:
let setA = new MySet();
setA.add('A');
setA.add('B');
setA.add('C');
setA.add('D');
setA.add('E');
setA.add('F');
let setB = new MySet();
setB.add('D');
setB.add('E');
setB.add('T');
setB.add('R');
setB.add('G');
setB.add('H');
setB.add('K');
setB.add('L');
例1、并集
//方法一:
//在MySet类中添加方法getUnionSet(otherSet)
console.log(MySet.getUnionSet(setA,setB));
//方法二:直接在外部写一个函数实现
function unionSet(setA,setB){
let set = new MySet();
setA.setValues().forEach(element => {
set.add(element);
});
setB.setValues().forEach(element => {
set.add(element);
})
return set;
}
console.log(unionSet(setA, setB));
例2、交集
//方法一:外部写
function intersectionSet(setA, setB) {
let set = new MySet();
setA.setValues().forEach(element => {
if (setB.hasOwn(element)) {
set.add(element);
}
});
return set;
}
console.log(intersectionSet(setA, setB));
//方法二:在MySet类中添加方法insertSection()
console.log(MySet.intersectionSet(setA, setB));
例3、差集
function difSet(setA, setB){
let set = new MySet();
setA.setValues().forEach(element => {
if(!setB.hasOwn(element)){
set.add(element);
}
});
return set;
}
console.log(difSet(setA,setB));
console.log(difSet(setB,setA));
console.log(MySet.differenceSet(setA,setB));
console.log(MySet.differenceSet(setB,setA));
例4、子集
function subSet(setA,setB){
//不能直接在forEach()中返回值,可以使用变量存放。
let result = true;
setB.setValues().forEach(element => {
if(!setA.hasOwn(element)){
result = false;
}
});
return result;
}
console.log(subSet(setA, setB));
console.log(MySet.subSet(setA, setB));
console.log(MySet.subSet(setB, setA));
例5、使用集合实现数组去重
思路:由于集合元素是不能重复的,只要将哦数组元素一次放入集合中,再依次取出就可以了
let arr = [1,2,3,4,5,1,4,5,2,6,7,8,9];
function arrRemoveRepeat(arr){
console.log(arr);
let myset = new MySet();
arr.forEach(element => {
myset.add(element);
});
let newArr = [];
newArr = myset.setValues();
return newArr;
}
console.log(arrRemoveRepeat(arr));