[LeetCode] 3731. Find Missing Elements

Chobby·2026년 1월 23일

LeetCode

목록 보기
954/992

😎풀이

  1. nums를 Set 객체로 변환
  2. nums 중 최댓값과 최솟값 탐색
  3. 최솟값 부터 최댓값까지 순회
    3-1. 기존 nums에 존재하지 않는 수 저장
  4. 저장된 수 반환
function findMissingElements(nums: number[]): number[] {
    const set = new Set(nums)
    const min = Math.min(...nums)
    const max = Math.max(...nums)
    const missing = []
    for(let i = min + 1; i < max; i++) {
        if(set.has(i)) continue
        missing.push(i)
    }
    return missing
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글