Skip to content

Conversation

@JustDevRae
Copy link
Collaborator

📌 푼 문제


📝 간단한 풀이 과정

중복된 문자 제거

문자열을 Set으로 변환한 후 배열로 바꿔 다시 문자열로 합치는 방식으로 해결했습니다.

function solution(my_string) {
  var answer = [...new Set(my_string)].join("");
  return answer;
}

한 번만 등장한 문자

Set을 사용해 문자의 등장 여부를 체크한 후, 중복된 문자는 별도의 Set에 저장하여 제거하는 방식으로 해결했습니다.

function solution(s) {
  const charSet = new Set();
  const duplicateSet = new Set();

  for (const char of s) {
    if (charSet.has(char)) {
      duplicateSet.add(char);
    } else {
      charSet.add(char);
    }
  }

  const uniqueChars = [...charSet].filter((char) => !duplicateSet.has(char));

  return uniqueChars.sort().join("");
}

무작위로 K개의 수 뽑기

Set을 활용하여 중복된 숫자를 제외하고 저장하고, 필요한 개수 k를 채웁니다. 만약 k개를 채우지 못하면 남은 부분을 -1로 채우는 방식으로 해결했습니다.

function solution(arr, k) {
  const uniqueSet = new Set();
  const result = [];

  for (const num of arr) {
    if (!uniqueSet.has(num)) {
      uniqueSet.add(num);
      result.push(num);
    }
    if (result.length === k) break;
  }

  while (result.length < k) {
    result.push(-1);
  }

  return result;
}

Contains Duplicate

Set을 활용하여 배열의 중복을 빠르게 확인하는 방식으로 해결했습니다.

var containsDuplicate = function (nums) {
  const numSet = new Set(nums);
  return numSet.size !== nums.length;
};

소인수분해

가장 작은 소수 2부터 시작하여 나누어 떨어질 때까지 나누고, 나눈 값을 저장하는 방식으로 해결했습니다. 중복된 소수는 Set을 사용하여 자동으로 제거되도록 처리했습니다.

function solution(n) {
  let result = [];
  let divisor = 2;

  while (n > 1) {
    if (n % divisor === 0) {
      result.push(divisor);
      n /= divisor;
    } else {
      divisor++;
    }
  }

  return [...new Set(result)];
}

Copy link
Collaborator

@Moonjonghoo Moonjonghoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체적으로 중복제거는 Set 자료구조를 활용하셔서 통일성있게 푸신거같아요!
한주간 고생하셨어요

@JooKangsan JooKangsan merged commit 6f730fe into main Feb 20, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants