Skip to content

Commit 2a57e46

Browse files
committed
Add uniq() to SequenceType
1 parent c959ce2 commit 2a57e46

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,18 @@ extension SequenceType where Generator.Element : Equatable {
583583
return split(maxSplit, allowEmptySlices: allowEmptySlices,
584584
isSeparator: { $0 == separator })
585585
}
586+
587+
/// Return an `Array` containing the elements of `self`
588+
@warn_unused_result
589+
public func uniq() -> [Generator.Element] {
590+
return reduce(Array<Generator.Element>()) { (arr, element) -> [Generator.Element] in
591+
if arr.contains({ $0 == element }) { return arr }
592+
593+
var newArr = arr
594+
newArr.append(element)
595+
return newArr
596+
}
597+
}
586598
}
587599

588600
extension SequenceType {

0 commit comments

Comments
 (0)