Skip to content

Commit b186667

Browse files
committed
refactor Array::unique to be more idiomatic
1 parent 1da06a8 commit b186667

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

chapters/arrays/removing-duplicate-elements-from-arrays.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ You want to remove duplicate elements from an array.
1111

1212
{% highlight coffeescript %}
1313
Array::unique = ->
14-
o = {}
15-
r = []
16-
o[this[i]] = this[i] for i in [1...@length]
17-
r.push(v) for k,v of o
18-
r
14+
unique = {}
15+
unique[@[key]] = @[key] for key in [1...@length]
16+
value for key, value of unique
1917

2018
[1,1,2,2,2,3,4,5,6,6,6,"a","a","b","d","b","c"].unique()
2119
# => [ 1, 2, 3, 4, 5, 6, 'a', 'b', 'd', 'c' ]

0 commit comments

Comments
 (0)