Description:
Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
uniqueInOrder([1,2,2,3,3]) == [1,2,3]
var uniqueInOrder=function(iterable){
let unique = [];
for (i=0; i < iterable.length; i++) {
if (iterable[i] !== iterable[i+1]) {
unique.push(iterable[i])
}
}
return unique;
}
먼저, 결과를 담은 값들을 넣을 빈 배열을 만들어주었다.
let unique = [];
그 다음에는 for문을 사용해서 현재 요소가 다음 요소와 같지않다면(현재 알파벳이 다음 알파벳이랑 같지않다면)
현재 알파벳을 빈 배열에 넣어준다.
for (i=0; i < iterable.length; i++) {
if (iterable[i] !== iterable[i+1]) {
unique.push(iterable[i])
}
}
'코테연습' 카테고리의 다른 글
37.Simple Pig Latin (0) | 2022.04.11 |
---|---|
36.Does my number look big in this? (0) | 2022.04.10 |
34.Decode the Morse code (0) | 2022.04.08 |
33.Tribonacci Sequence (0) | 2022.04.07 |
32.Your order, please (0) | 2022.04.07 |
댓글