-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuniqueInOrder-codewar.js
52 lines (40 loc) · 1.17 KB
/
uniqueInOrder-codewar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*Description:
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]
*/
//mine
var uniqueInOrder = function(iterable) {
//your code here - remember iterable can be a string or an array
if (typeof(iterable) === 'string')
var iterable = iterable.split('');
var len = iterable.length;
if (len === 0) return [];
var newArray = [iterable[0]];
for (var i = 1; i < len; i++) {
var temp = iterable[i];
if (temp !== iterable[i - 1]) {
newArray.push(temp);
}
}
return newArray;
}
//others
var uniqueInOrder = function(iterable) {
return [].filter.call(iterable, (function(a, i) {
return iterable[i - 1] !== a
}));
}
//others
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
}