[codewars] Return the Missing Element

sekoong·2024년 9월 20일

CodeWars

목록 보기
5/6
post-thumbnail

7kyu

Instruction

Fellow code warrior, we need your help! We seem to have lost one of our sequence elements, and we need your help to retrieve it!

Our sequence given was supposed to contain all of the integers from 0 to 9 (in no particular order), but one of them seems to be missing.

Write a function that accepts a sequence of unique integers between 0 and 9 (inclusive), and returns the missing element.

Examples:
[0, 5, 1, 3, 2, 9, 7, 6, 4] --> 8
[9, 2, 4, 5, 7, 0, 8, 6, 1] --> 3

My Solution

function getMissingElement(superImportantArray){
  
  let fullVal = [0, 5, 1, 3, 2, 9, 7, 6, 4, 8]; 
  return fullVal.filter(value =>  !superImportantArray.includes(value))[0];

}

Other Solution

function getMissingElement(superImportantArray){
  for (i = 0; i < 10; i++) {
    if (superImportantArray.indexOf(i) === -1) return i;
  }
}

review

처음엔 for문만을 생각했다가 조금더 간단한 방법은 없을지 고민하기 시작함.
배열.filter(), 배열.map()의 개념을 적용해봐야겠다 싶어서 다시 정리하고 적용해봤다.
조금씩 모르는 개념은 정리하고 문제에 적용시키는 방식이 익숙해지고 있는 것 같다.
그리고 답을 제출한 후에 다른 로직을 보고 반성의 시간을 가지고...
indexOf()의 개념 또한 몰랐기 때문에 other solution을 보고 모르는 부분은 정리해보았다.
문제를 풀면서 개념을 익히니 확실히 기억에 잘 남는다! 시간이 걸리더라도 내가 납득이 가고 안보고 로직을
작성 할 수 있을 때까지 제대로 정리해야 겠다고 느꼈다.
그리고 익힌 개념들을 응용해서 다른 문제도 풀어봐야 겠다!
아직 갈길이 먼거같다^^..~

0개의 댓글