writing a function that return a list without any elements that are the same nex
ID: 3823083 • Letter: W
Question
writing a function that return a list without any elements that are the same next to each other, maintaining the original order. having some issues getting work correctly, here's what i have so far, the function needs to use two counters.
var order = function(iterable) {
var freq = [ ];
var count = 1;
for(var i = 0; i < iterable.length; i++) {
count = 1;
}
for(var j = i+1; j < iterable.length; j++) {
if(iterable[i] == iterable[j]) {
count++;
}
else {
freq.push(iterable[j]);
}
return freq;
}
}
order([A,A,a,B,B,c, A, D, D]); (should output: [A,a,B,c,a,D])
Explanation / Answer
HI, Please find my correct implementation.
Please let me know in case of any issue.
var order = function(iterable) {
var freq = [ ];
for(var i = 0; i < iterable.length; i++) {
// pushing current element
freq.push(iterable[i]);
// skipping similar consecutive elements
while((i < iterable.length-1) && (arr[i] == arr[i+1])){
i++;
}
}
return freq;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.