알고리즘 60 - Unique In Order

박진현·2021년 7월 21일
0

Q.

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]

A)

var uniqueInOrder=function(iterable){
  //your code here - remember iterable can be a string or an array
  let res = [];
  for (let i = 0; i < iterable.length ; i++) {
    if ( iterable[i] === iterable[i-1]) {
      continue;
    }
    if ( iterable[i] !== iterable[i-1]) {
      console.log(i)
      res.push(iterable[i])
    }
  }
  return res
}
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글