From 6962c914a9fae87df68ab5370e2122f143574371 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 16 May 2022 08:06:12 -0400 Subject: [PATCH] document unique(i -> a[i], eachindex(a)) trick (#45291) Co-authored-by: Milan Bouchet-Valat --- base/set.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/base/set.jl b/base/set.jl index b77d39e80ae5d..80b0138eb89f9 100644 --- a/base/set.jl +++ b/base/set.jl @@ -195,6 +195,28 @@ julia> unique(x -> x^2, [1, -1, 3, -3, 4]) 3 4 ``` +This functionality can also be used to extract the *indices* of the first +occurrences of unique elements in an array: +```jldoctest +julia> a = [3.1, 4.2, 5.3, 3.1, 3.1, 3.1, 4.2, 1.7]; + +julia> i = unique(i -> a[i], eachindex(a)) +4-element Vector{Int64}: + 1 + 2 + 3 + 8 + +julia> a[i] +4-element Vector{Float64}: + 3.1 + 4.2 + 5.3 + 1.7 + +julia> a[i] == unique(a) +true +``` """ function unique(f, C; seen::Union{Nothing,Set}=nothing) out = Vector{eltype(C)}()