Data Structures & Algorithms with Javascript : Hashing
[์ด์ ๊ธ] 2024.07.22 - [๐ค data structures & algorithms] - Data Structures & Algorithms with Javascript : DictionariesHashing์ ๋ฐ์ดํฐ ์ถ๊ฐ์ ๋ฐํ์ ๋น ๋ฅด๊ฒ ํ๊ธฐ ์ํ ๋ฐ์ดํฐ ์ ์ฅ ๊ธฐ์ ์ด๋ค.Hash table์ด๋ผ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ
pyotato-dev.tistory.com
set์ ์ค๋ณต๋๋ ์์(member)๊ฐ ์๊ณ ์์ ๊ฐ์ ์์๊ฐ ์๋ ์งํฉ์ด๋ค.
๐ฉ๐ป๐ป์ค๋์ ๋ด์ฅ๋ Set()์ ํ๋ฒ ์ง์ ํด๋์ค๋ก ๊ตฌํํด ๋ณด์.
Set์ ์ ์
- ์์๊ฐ ์๋ set์ empty set(๊ณต์งํฉ)์ด๋ผ๊ณ ํ๋ค. universe(์ ์ฒด ์งํฉ)๋ ๊ฐ๋ฅํ ๋ชจ๋ ์์ set์ ๊ฐ๋ฆฌํจ๋ค.
- ๋ set์ด ๋์ผํ ์์๋ฅผ ์ง๋๋ค๋ฉด ์๋ก ๊ฐ๋ค.
- ํ๋์ set B๊ฐ ๋ค๋ฅธ set A์ ์์๋ค์ ํฌํจํ๋ค๋ฉด set A๋ set B์ subset(๋ถ๋ถ ์งํฉ)์ด๋ค.
Set ์ฐ์ฐ
- Union : ๋ set์ ๋ชจ๋ ์์๋ค (ํฉ์นฉํฉ)
- Intersection : ๋ set์ ์์๋ค ์ค๊ณตํต ์์๋ค (๊ต์งํฉ)
- Difference : ๋ set ์ค ๊ต์งํฉ์ ์ ์ธํ ์์๋ค(์ฐจ์งํฉ)
Set ํด๋์ค ๊ตฌํ
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ํด๋์ค๋ก ์ฌ๊ตฌ์ฑํ์ต๋๋ค!!
class Set{
constructor(){
this.dataStore = [];
}
// ์ด๋ฏธ ์๋ ์์๊ฐ ์๋๋ผ๋ฉด ์์ ์ถ๊ฐ
add(data){
if (this.dataStore.indexOf(data) < 0) {
this.dataStore.push(data);
return true;
} else {
return false;
}
}
// ์์ ์ ๊ฑฐ
remove(data){
let pos = this.dataStore.indexOf(data);
if (pos > -1) {
this.dataStore.splice(pos,1);
return true;
} else {
return false;
}
}
// ์์ ๋ณด๊ธฐ
show(){
return this.dataStore;
}
// ์์๊ฐ set์ ์๋ ์ง ํ์ธํ๋ ํฌํผ ํจ์
contains(data) {
if (this.dataStore.indexOf(data) > -1) {
return true;
} else {
return false;
}
}
// ํฉ์งํฉ
union(){
let tempSet = new Set();
for (let i = 0; i < this.dataStore.length; ++i) {
tempSet.add(this.dataStore[i]);
}
for (let i = 0; i < set.dataStore.length; ++i) {
if (!tempSet.contains(set.dataStore[i])) {
tempSet.dataStore.push(set.dataStore[i]);
}
}
return tempSet;
}
//๊ต์งํฉ
intersect(set){
let tempSet = new Set();
for(let i=0; i< this.dataStore.length; ++i){
if(set.contains(this.dataStore[i])){
tempSet.add(this.dataStore[i]);
}
}
return tempSet;
}
// set์ ํฌ๊ธฐ
size() {
return this.dataStore.length;
}
//๋ถ๋ถ์งํฉ
subset(set){
if (this.size() > set.size()) {
return false;
} else {
return this.dataStore.every(member=>
set.contains(member)
)
}
}
// ์ฐจ์งํฉ
difference(set){
let tempSet = new Set();
for(let i = 0; i < this.dataStore.length; ++i){
if(!set.contains(this.dataStore[i])){
tempSet.add(this.dataStore[i]);
}
}
return tempSet;
}
}
const print = item => console.log(item);
const set1 = new Set();
set1.add("Mike");
set1.add("Clayton");
set1.add("Jennifer");
set1.add("Raymond");
print(set1.show());
const set2 = new Set();
set2.add("Raymond");
set2.add("Cynthia");
set2.add("Bryan");
print(set2.show()); // ['Raymond', 'Cynthia', 'Bryan']
const intersection = set1.intersect(set2);
print(`intersection: ${intersection.show()}`); // intersection: Raymond
const setA = new Set();
setA.add("Cynthia"); setA.add("Clayton"); setA.add("Jennifer"); setA.add("Danny"); setA.add("Jonathan"); setA.add("Terrill"); setA.add("Raymond"); setA.add("Mike");
print(set1.show()); // ['Mike', 'Clayton', 'Jennifer', 'Raymond']
const setB = new Set();
setB.add("Cynthia");
setB.add("Raymond");
setB.add("Jonathan");
if (setB.subset(setA)) {
print("setB is a subset of setA.");
} else {
print("setB is not a subset of setA.");
}
// setB is a subset of setA.
setB.add("Shirley"); // add Shirley to setB
print("add Shirley to setB"); // setB is not a subset of setA.
if (setB.subset(setA)) {
print("setB is a subset of setA.");
} else {
print("setB is not a subset of setA.");
}
const difference = setA.difference(setB);
print(`setA[${setA.show()}] difference setB[${setB.show()}] -> ${difference.show()}`);
// setA[Cynthia,Clayton,Jennifer,Danny,Jonathan,Terrill,Raymond,Mike] difference setB[Cynthia,Raymond,Jonathan,Shirley] -> Clayton,Jennifer,Danny,Terrill,Mike
๐ Reference
- Data Structures and Algorithms Using Javaโ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9