๐ Day 10 Algorithm Review
26107 ใฏใผใ
๐ Replit
โก๏ธ restParameter
// ์ธ์๋ก ์ ๋ฌ๋ ์ซ์๋ค์ ์ดํจ์ ๊ตฌํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์.
function sum4(...args){
let total = 0;
// for (let x of args){
// total = total + x;
// }
const reducer = (prev, cur) => {
}
args.reduce(reducer)
return total
}
console.log(sum4(5,7,2)); // 14
// ========= reduce ์ฌ์ฉํด์ ํ์ด๋ณด๊ธฐ =========
function sum4(...args){
let total = 0;
console.log(args)
const reducer = (prev, cur) => {
console.log(prev)
return prev + cur
}
total = args.reduce(reducer)
return total
}
console.log(sum4(5,7,2)); // 14
โก๏ธ new Set
// ๋ฌธ์์ด๋ก ์ด๋ฃจ์ด์ง ๋ ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋ ๋ ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ๊ณ ์๋ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์.
function common(arr1, arr2){
// Set ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์.
arr1 = new Set(arr1)
arr2 = new Set(arr2)
let result = new Set([...arr1].filter(x => arr2.has(x)))
console.log([...result])
return result.size
}
let a = ['a', 'b', 'c', 'c', 'b'];
let b = ['b', 'b', 'b', 'c', 'e', 'e', 'f'];
console.log(common(a, b)); // 2
โก๏ธ new Map
// ์ธ์๋ก ๋ฐ์ ๋ฌธ์์ด์ ๋ถ๋ฅํด์ ๊ธฐ์กด์ ์๋ ์ปฌ๋ ์
์ ๊ฐ์๋ฅผ ๋ํด์ฃผ๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ฃผ์ธ์.
function classification(str){
let map = new Map([['A',1],['B',2],['C',3]]);
// Map๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด๋ณด์ธ์.
for (let i = 0; i<str.length; i++){
switch(str[i]){
case 'A':
map.set('A', map.get('A') + 1)
break
case 'B':
map.set('B', map.get('B') + 1)
break
case 'C':
map.set('C', map.get('C') + 1)
break
}
}
return map;
}
var str = "ABCCCAA"
console.log(classification(str)); //Map(3){'A' => 4, 'B' => 3, 'C' => 6}
๐ Vote
function vote(str){
// ์๋์ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์ธ์.
str.split(" ")
let count = {}
for (let a of str){
if (a in count){
count[a] += 1
} else {
count[a] = 1
}
}
let value = Object.values(count)
let max = value[0]
let maxIndex = 0
for(let i = 0; i < value.length; i++){
if (value[i] > max){
max = value[i]
maxIndex = i
}
}
return Object.keys(count)[maxIndex]
}
vote("BACBACCACCBDEDE") // 'C'
// =============== new Map ์ฌ์ฉ ===============
//assignment
function vote(str){
// ์๋์ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์ธ์.
const strList = [... new Set(str.split('').sort())]
const arr = Array.from(Array(strList.length), (e, i) => [strList[i],0])
let mapVote = new Map(arr)
str.split('').forEach((e) => {
if(mapVote.has(e)){
mapVote.set(e, mapVote.get(e) + 1)
}
})
let answer = [...mapVote].reduce((a,b) => a[1] < b[1] ? b : a)[0]
return answer
}
vote("BACBACCACCBDEDE") // 'C'
// =============== Mento ===============
function vote(str){
var sH = new Map()
// ๊ฐ๊ฐ ๋ชํ
for(let s of str){
if (sH.has(s)){
sH.set(s, sH.get(s) + 1)
} else {sH.set(s,1)
}
}
let max = 0;
let answer
// ๋๊ฐ ์ ์ผ ๋ง์ด
for (let [k,v] of sH){
if (v>max){
max = v
answer = k
}
}
return answer
}
vote("BACBACCACCBDEDE") // 'C'
Reference
ใใฎๅ้กใซใคใใฆ(๐ Day 10 Algorithm Review), ๆใ
ใฏใใใๅคใใฎๆ
ๅ ฑใใใใง่ฆใคใใพใใ
https://velog.io/@j00b33/BE-Day-10-Algorithm-Review
ใใญในใใฏ่ช็ฑใซๅ
ฑๆใพใใฏใณใใผใงใใพใใใใ ใใใใฎใใญใฅใกใณใใฎURLใฏๅ่URLใจใใฆๆฎใใฆใใใฆใใ ใใใ
Collection and Share based on the CC Protocol
// ์ธ์๋ก ์ ๋ฌ๋ ์ซ์๋ค์ ์ดํจ์ ๊ตฌํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์.
function sum4(...args){
let total = 0;
// for (let x of args){
// total = total + x;
// }
const reducer = (prev, cur) => {
}
args.reduce(reducer)
return total
}
console.log(sum4(5,7,2)); // 14
// ========= reduce ์ฌ์ฉํด์ ํ์ด๋ณด๊ธฐ =========
function sum4(...args){
let total = 0;
console.log(args)
const reducer = (prev, cur) => {
console.log(prev)
return prev + cur
}
total = args.reduce(reducer)
return total
}
console.log(sum4(5,7,2)); // 14
// ๋ฌธ์์ด๋ก ์ด๋ฃจ์ด์ง ๋ ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋ ๋ ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ๊ณ ์๋ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ์ธ์.
function common(arr1, arr2){
// Set ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์.
arr1 = new Set(arr1)
arr2 = new Set(arr2)
let result = new Set([...arr1].filter(x => arr2.has(x)))
console.log([...result])
return result.size
}
let a = ['a', 'b', 'c', 'c', 'b'];
let b = ['b', 'b', 'b', 'c', 'e', 'e', 'f'];
console.log(common(a, b)); // 2
// ์ธ์๋ก ๋ฐ์ ๋ฌธ์์ด์ ๋ถ๋ฅํด์ ๊ธฐ์กด์ ์๋ ์ปฌ๋ ์
์ ๊ฐ์๋ฅผ ๋ํด์ฃผ๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ฃผ์ธ์.
function classification(str){
let map = new Map([['A',1],['B',2],['C',3]]);
// Map๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด๋ณด์ธ์.
for (let i = 0; i<str.length; i++){
switch(str[i]){
case 'A':
map.set('A', map.get('A') + 1)
break
case 'B':
map.set('B', map.get('B') + 1)
break
case 'C':
map.set('C', map.get('C') + 1)
break
}
}
return map;
}
var str = "ABCCCAA"
console.log(classification(str)); //Map(3){'A' => 4, 'B' => 3, 'C' => 6}
function vote(str){
// ์๋์ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์ธ์.
str.split(" ")
let count = {}
for (let a of str){
if (a in count){
count[a] += 1
} else {
count[a] = 1
}
}
let value = Object.values(count)
let max = value[0]
let maxIndex = 0
for(let i = 0; i < value.length; i++){
if (value[i] > max){
max = value[i]
maxIndex = i
}
}
return Object.keys(count)[maxIndex]
}
vote("BACBACCACCBDEDE") // 'C'
// =============== new Map ์ฌ์ฉ ===============
//assignment
function vote(str){
// ์๋์ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์ธ์.
const strList = [... new Set(str.split('').sort())]
const arr = Array.from(Array(strList.length), (e, i) => [strList[i],0])
let mapVote = new Map(arr)
str.split('').forEach((e) => {
if(mapVote.has(e)){
mapVote.set(e, mapVote.get(e) + 1)
}
})
let answer = [...mapVote].reduce((a,b) => a[1] < b[1] ? b : a)[0]
return answer
}
vote("BACBACCACCBDEDE") // 'C'
// =============== Mento ===============
function vote(str){
var sH = new Map()
// ๊ฐ๊ฐ ๋ชํ
for(let s of str){
if (sH.has(s)){
sH.set(s, sH.get(s) + 1)
} else {sH.set(s,1)
}
}
let max = 0;
let answer
// ๋๊ฐ ์ ์ผ ๋ง์ด
for (let [k,v] of sH){
if (v>max){
max = v
answer = k
}
}
return answer
}
vote("BACBACCACCBDEDE") // 'C'
Reference
ใใฎๅ้กใซใคใใฆ(๐ Day 10 Algorithm Review), ๆใ ใฏใใใๅคใใฎๆ ๅ ฑใใใใง่ฆใคใใพใใ https://velog.io/@j00b33/BE-Day-10-Algorithm-Reviewใใญในใใฏ่ช็ฑใซๅ ฑๆใพใใฏใณใใผใงใใพใใใใ ใใใใฎใใญใฅใกใณใใฎURLใฏๅ่URLใจใใฆๆฎใใฆใใใฆใใ ใใใ
Collection and Share based on the CC Protocol