Last active
May 29, 2024 12:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
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
}
thanks this helps
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
Hi, it should be something like that: