Notice
Recent Posts
Recent Comments
Link
코드로 우주평화
Unique In Order 본문
반응형
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]
Solution:
1. Puts the first element of a string into an array.
2. Repeat the rest of the strings.
3. If the repeated character is not the same as the previous character, it is put into an array.
4. When the iteration is finished, an array is returned.
var uniqueInOrder=function(iterable){
let result = []
let i = 0
if (iterable){
result.push(iterable[i])
}
for (const value of iterable.slice(1,)){
if (iterable[i] != value){
result.push(value)
}
i++
}
return result
}
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Growth of a Population (0) | 2022.03.01 |
---|---|
Binary Addition (0) | 2022.02.27 |
Find the next perfect square! (0) | 2022.02.25 |
Exes and Ohs (0) | 2022.02.24 |
Detect Pangram (0) | 2022.02.23 |