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.
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
defunique_in_order(iterable) iterable.is_a?(String) ? iterable.split('').chunk{ |x| x }.map{ |x, ary| x } : iterable.chunk{ |x| x }.map{ |x, ary| x } end
Solution(2):
1 2 3 4 5
defunique_in_order(iterable) (iterable.is_a?(String) ? iterable.chars : iterable ) .chunk { |x| x }.map{ |x| x } end
JavaScript
Solution(1):
1 2 3 4 5 6 7 8 9 10 11 12
var uniqueInOrder = function(iterable){ var result = []; var last = '';
for(var i = 0; i < iterable.length; i++){ if(iterable[i] !== last){ last = iterable[i] result.push(last); } } return result; }
Solution(2):
1 2 3
var uniqueInOrder=function(iterable){ return iterable.split('').filter((a, i) => a !== iterable[i-1]) }
Solution(3):
1 2 3
var uniqueInOrder=function(iterable){ return [...iterable].filter((a, i) => a !== iterable[i-1]) }