# How can write a function to find unique elements in array without any allocation?

**URL:** https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005
**Category:** General Usage
**Created:** [January 31, 2020, 3:07am UTC](https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005 "2020-01-31T03:07:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)
#### Post date: [January 31, 2020, 3:07am UTC](https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005/1 "2020-01-31T03:07:14Z")

</div>

Hi,

I am using `unique` function to find unique elements in an array. But the memory allocation to use this function is increasing with array size. In my case array size is nearly 10000, and it causes 36 allocation.

> a = rand(10000)  
> @btime unique(a)  
> 335.624 μs (36 allocations: 450.50 KiB)

Moreover, I have to use this function multiple times, more than 10M times. So, is there any other methods to reduce the allocations in `unique` function? or Can you suggest some ideas to `unique` function with less allocation with similar execution time?  
Thanks in Advance !  
Manu

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [January 31, 2020, 3:47am UTC](https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005/2 "2020-01-31T03:47:53Z")

</div>

[from the docs](https://docs.julialang.org/en/v1/base/collections/), try:

```julia
a = rand(10000)
sort!(a)
unique!(a)

```

`rand(10000)` is a bad example as is probably unique anyway, but it’s a step.  
Other option is using `Set(a)`

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 31, 2020, 5:15am UTC](https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005/3 "2020-01-31T05:15:15Z")

</div>

Also, interpolate `a` into the benchmark to avoid a measurement of the global lookup: `@btime unique($a)`.

---

<div class="post-metadata">

### Author: ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)
#### Post date: [January 31, 2020, 5:59am UTC](https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005/4 "2020-01-31T05:59:13Z")

</div>

@longemen3000 @carstenbauer Thanks for your reply. When I applied your suggestion, allocations reduced as follows:

```julia
@btime unique!($a)
 286.947 μs (23 allocations: 193.88 KiB)

```

Thank You,  
Manu

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 31, 2020, 6:07am UTC](https://discourse.julialang.org/t/how-can-write-a-function-to-find-unique-elements-in-array-without-any-allocation/34005/5 "2020-01-31T06:07:32Z")

</div>

if you don’t mind the result being sorted, try `@btime unique!(sort!($a))`

```julia
julia> a=rand(1:1_000,10_000); b=copy(a);

julia> @btime unique!($a);
  22.201 μs (18 allocations: 49.58 KiB)

julia> @btime (unique!(sort!($b)));
  7.000 μs (0 allocations: 0 bytes)

```
