DAY 2
1

# 02 - Unique In Order

Don't say so much, just coding...

## Instruction

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]
``````

### Ruby

#### Init

``````  def unique_in_order(iterable)
# put your solution here
end
``````

#### Sample Testing

``````  Test.assert_equals(unique_in_order('AAAABBBCCDAABBB'), ['A','B','C','D','A','B'])
``````

### Javascript

#### Init

``````  var uniqueInOrder = function(iterable){
// your code here - remember iterable can be a string or an array
}
``````

#### Sample Testing

``````  Test.assertSimilar(uniqueInOrder('AAAABBBCCDAABBB'), ['A','B','C','D','A','B'])
``````

# Solution

### Ruby

``````  # Solution 1
def unique_in_order(iterable)
(iterable.is_a?(String) ? iterable.split('') : iterable)
.chunk{ |s| s }
.map(&:first)
end

# Solution 2
def unique_in_order(iterable)
(iterable.is_a?(String) ? iterable.chars : iterable)
.chunk { |s| s }
.map(&:first)
end

# Solution 3
def unique_in_order(iterable)
iterable.is_a?(String) ? iterable.squeeze.split('') : iterable.uniq
end
``````

### Javascript

``````  // Solution 1
var uniqueInOrder = function(iterable){
var result = [];
for(i = 0; i < iterable.length; i++) {
if (iterable[i] != iterable[i+1]) result.push(iterable[i]);
}
return result;
}

// Solution 2
var uniqueInOrder = function(iterable){
return [...iterable].filter((i, index) => i !== iterable[index-1])
}
``````