데이터 구조 5 - 집합
5529 단어 데이터 구조
// set
function Set(){
this.items = {}
//
Set.prototype.has = function(value){
return this.items.hasOwnProperty(value);
}
//
Set.prototype.add= function(value){
if(this.has(value)) return false;
this.items[value] =value;
}
//
Set.prototype.del = function (value) {
if(!this.has(value)) return false;
delete this.items[value];
}
//
Set.prototype.clear = function(){
this.items ={}
}
//
Set.prototype.size = function () {
return Object.keys(this.items).length
}
//
Set.prototype.getValue = function () {
return Object.values(this.items);
}
}
let set = new Set();
set.add('aaa');
set.add('bbb');
console.log(set);//Set { items: { aaa: 'aaa', bbb: 'bbb' } }
console.log(set.has('aaa'));//true
console.log(set.size());//2
console.log(set.getValue());//[ 'aaa', 'bbb' ]
집합
두 집합 중 중복 되 지 않 는 요 소 를 새로운 집합 에 추가 합 니 다.
// set
function Set(){
this.items = {}
//
Set.prototype.has = function(value){
return this.items.hasOwnProperty(value);
}
//
Set.prototype.add= function(value){
if(this.has(value)) return false;
this.items[value] =value;
}
//
Set.prototype.del = function (value) {
if(!this.has(value)) return false;
delete this.items[value];
}
//
Set.prototype.clear = function(){
this.items ={}
}
//
Set.prototype.size = function () {
return Object.keys(this.items).length
}
//
Set.prototype.getValue = function () {
return Object.values(this.items);
}
/*
:
*/
Set.prototype.unionSet = function (setB) {
let newSet = new Set();
let values = this.getValue();// A
for(let i=0;i
집합
// set
function Set(){
this.items = {}
//
Set.prototype.has = function(value){
return this.items.hasOwnProperty(value);
}
//
Set.prototype.add= function(value){
if(this.has(value)) return false;
this.items[value] =value;
}
//
Set.prototype.del = function (value) {
if(!this.has(value)) return false;
delete this.items[value];
}
//
Set.prototype.clear = function(){
this.items ={}
}
//
Set.prototype.size = function () {
return Object.keys(this.items).length
}
//
Set.prototype.getValue = function () {
return Object.values(this.items);
}
/*
:
*/
Set.prototype.intersection = function (setB) {
let newSet = new Set();
let values = this.getValue();
for(let i = 0 ; i
집합 차 집
// set
function Set(){
this.items = {}
//
Set.prototype.has = function(value){
return this.items.hasOwnProperty(value);
}
//
Set.prototype.add= function(value){
if(this.has(value)) return false;
this.items[value] =value;
}
//
Set.prototype.del = function (value) {
if(!this.has(value)) return false;
delete this.items[value];
}
//
Set.prototype.clear = function(){
this.items ={}
}
//
Set.prototype.size = function () {
return Object.keys(this.items).length
}
//
Set.prototype.getValue = function () {
return Object.values(this.items);
}
/*
: A B
*/
Set.prototype.differentSet = function (setB) {
let newSet = new Set();
let values = this.getValue();
for(let i = 0 ; i
집합 부분 집합
집합 A 는 집합 B 의 부분 집합 이다.
// set
function Set(){
this.items = {}
//
Set.prototype.has = function(value){
return this.items.hasOwnProperty(value);
}
//
Set.prototype.add= function(value){
if(this.has(value)) return false;
this.items[value] =value;
}
//
Set.prototype.del = function (value) {
if(!this.has(value)) return false;
delete this.items[value];
}
//
Set.prototype.clear = function(){
this.items ={}
}
//
Set.prototype.size = function () {
return Object.keys(this.items).length
}
//
Set.prototype.getValue = function () {
return Object.values(this.items);
}
/*
: A B
*/
Set.prototype.subSet = function (setB) {
let values = this.getValue();
for(let i = 0 ; i