Skip to content

Commit 6962c91

Browse files
stevengjnalimilan
andauthored
document unique(i -> a[i], eachindex(a)) trick (JuliaLang#45291)
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
1 parent 7e37de4 commit 6962c91

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

base/set.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,28 @@ julia> unique(x -> x^2, [1, -1, 3, -3, 4])
195195
3
196196
4
197197
```
198+
This functionality can also be used to extract the *indices* of the first
199+
occurrences of unique elements in an array:
200+
```jldoctest
201+
julia> a = [3.1, 4.2, 5.3, 3.1, 3.1, 3.1, 4.2, 1.7];
202+
203+
julia> i = unique(i -> a[i], eachindex(a))
204+
4-element Vector{Int64}:
205+
1
206+
2
207+
3
208+
8
209+
210+
julia> a[i]
211+
4-element Vector{Float64}:
212+
3.1
213+
4.2
214+
5.3
215+
1.7
216+
217+
julia> a[i] == unique(a)
218+
true
219+
```
198220
"""
199221
function unique(f, C; seen::Union{Nothing,Set}=nothing)
200222
out = Vector{eltype(C)}()

0 commit comments

Comments
 (0)