๐ŸŒŒ Day 10 Algorithm Review


๐ŸŒš 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'