Skip to content

Instantly share code, notes, and snippets.

@mdixon47
Last active May 29, 2024 12:32
Show Gist options
  • Select an option

  • Save mdixon47/33cf090678481de272ce8e6e3167b752 to your computer and use it in GitHub Desktop.

Select an option

Save mdixon47/33cf090678481de272ce8e6e3167b752 to your computer and use it in GitHub Desktop.
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.
function uniqueInOrder(it) {
var result = []
var last
for (var i = 0; i < it.length; i++) {
if (it[i] !== last) {
result.push(last = it[i])
}
}
return result
}
@damianjnc
Copy link

Hi, it should be something like that:

functionuniqueInOrder(iterable){
  //your code here - remember iterable can be a string or an array
  const result = []
  
  for(let i = 0; i < iterable.length; i++){
    if(iterable[i] !== iterable[i + 1]){
      result.push(iterable[i])
    }
  }
  
  return result
}

@Nyambura254
Copy link

this is the right way

function uniqueInOrder(iterable){
//your code here - remember iterable can be a string or an array
const result = [ ]

for(let i = 0; i < iterable.length; i++){
if(iterable[ i ] !== iterable[i + 1]){
result.push(iterable[ i ])
}
}

return result
}

@christianwondeson
Copy link

thanks this helps

@ChrisKeith01
Copy link

I'm very confused I tried both methods and my code failed both tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment