🌌 Day 10 Algorithm Review

JBΒ·2022λ…„ 3μ›” 25일
0

Algorithms

λͺ©λ‘ 보기
11/12

🌚 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'

profile
두비두λ°₯λ°₯

0개의 λŒ“κΈ€