# Opposite of unique

**URL:** <https://discourse.julialang.org/t/opposite-of-unique/57802>\
**Category:** New to Julia\
**Tags:** sets\
**Created:** [March 23, 2021, 4:58pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802 "2021-03-23T16:58:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [March 23, 2021, 4:58pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/1 "2021-03-23T16:58:58Z")

</div>

Hi!  
Is there a function that does the opposite of unique? Say

```julia
nonunique([1,2,3,4,3,5,3,2]) = [2,3]

```

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 23, 2021, 5:08pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/2 "2021-03-23T17:08:35Z")

</div>

```julia
julia> using DataStructures

julia> nonunique(v) = [k for (k, v) in counter(v) if v > 1]
nonunique (generic function with 1 method)

julia> nonunique(v)
2-element Vector{Int64}:
 2
 3

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 23, 2021, 5:11pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/3 "2021-03-23T17:11:31Z")

</div>

Equivalently to the above, you can use `countmap` from `StatsBase` instead of `counter` from `DataStructures`:

```julia
using StatsBase
[k for (k, v) in countmap(v) if v > 1]

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 23, 2021, 7:02pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/4 "2021-03-23T19:02:59Z")

</div>

For fun, code for this without using any packages and with only passing over the data once.  
This is kinda ugly because i am golfing it at bit:

```julia
function nonunique(v)
    seen = Dict{eltype(v), Ref{Int}}()
    [x for x in v if 2 == (get!(()->Ref(0), seen, x)[]+=1)]
end

```

which does (still unsorted could sort after)

```julia
julia> nonunique([1,2,3,4,3,5,3,2])
2-element Array{Int64,1}:
 3
 2

```

another fun way: use `sort` then `diff` to find things that occur after things that are the same as them, then `unique` to drop extra multiples

```julia
function nonunique(v)
    sv = sort(v)
    return unique(@view sv[[diff(sv).==0; false]])
end

```

I’ld probably use one of the packages and two passes though.  
Or a `Dict` and two passes

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 23, 2021, 7:08pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/5 "2021-03-23T19:08:24Z")

</div>

That’s not quite the opposite to `unique`. Note that `unique([1,2,3,4,3,5,3,2]) == [1,2,3,4,5]`, not `[1,4,5]`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 23, 2021, 7:48pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/6 "2021-03-23T19:48:12Z")

</div>

Maybe a better name would be `only_repeated_values` or something like that.

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [March 23, 2021, 8:22pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/7 "2021-03-23T20:22:06Z")

</div>

@stillyslalom @CameronBieganek @oxinabox thanks for coding something for me! I was just asking if there was a built in function, didn’t expect you to write the solution 🙂  
I ended up using `symdiff(v,unique(v))`, which works for my specific case (no more than 2 of the same number, and I also need unique, so that is available for free).  
Thanks again!

---

<div class="post-metadata">

**Author:** ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)\
**Post date:** [March 23, 2021, 10:52pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/8 "2021-03-23T22:52:13Z")

</div>

R calls this function `duplicated`. Rather, R’s `duplicated()` would return the indices of the second 2 and second and third 3 in the Ribiero’s example. Then again, R was never known for having overly-descriptive function names. 😉

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 24, 2021, 12:04am UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/9 "2021-03-24T00:04:27Z")

</div>

Using the beautiful [Multisets.jl](https://github.com/scheinerman/Multisets.jl) package:

```julia
using Multisets
v = [1,2,3,4,3,5,3,2]
M = Multiset(v)
U = Multiset(Set(M))
collect(keys(M-U))

2-element Vector{Int64}:
 2
 3

```

And another way:

```julia
using Multisets
v = [1,2,3,4,3,5,3,2]
M = Multiset(v)
collect(keys(M))[values(M).>1]

2-element Vector{Int64}:
 2
 3

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 24, 2021, 10:51am UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/10 "2021-03-24T10:51:48Z")

</div>

On this topic, see also the fast solutions by Przemyslaw Szufel and Bogumił Kamiński in [stackoverflow](https://stackoverflow.com/questions/54652787/julia-function-to-return-non-unique-elements-of-an-array).

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 24, 2021, 12:57pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/11 "2021-03-24T12:57:32Z")

</div>

NB! log y axis  
 ![results](https://global.discourse-cdn.com/julialang/original/3X/d/c/dcffb4b9e2c53b797d3ec015dff4bd26ad87b4ba.png)

```julia
results
9-element BenchmarkTools.BenchmarkGroup:
  tags: []
  "dict" => Trial(357.094 μs)
  "symdiff" => Trial(473.081 μs)
  "szufelinplace" => Trial(39.201 μs)
  "countmap" => Trial(229.767 μs)
  "counter" => Trial(196.279 μs)
  "multiset1" => Trial(792.868 μs)
  "sort" => Trial(65.060 μs)
  "szufel" => Trial(42.194 μs)
  "multiset2" => Trial(429.816 μs)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 24, 2021, 2:20pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/12 "2021-03-24T14:20:54Z")

</div>

@gustaphe, very nice summary but what a weird logarithmic scale axis that one is (with ticks at `10^4.8`, etc.). Integer powers would be easier to read.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 24, 2021, 2:39pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/13 "2021-03-24T14:39:38Z")

</div>

Yeah, for some reason that’s the default behaviour in GR. You can set the ticks manually, but I didn’t feel like it.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 24, 2021, 7:46pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/14 "2021-03-24T19:46:29Z")

</div>

How’s this for effort? 😛  
 ![results](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b4c4a319c0e5af0c709976278dbcb48a8bc7de8.png)

---

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [March 24, 2021, 7:57pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/15 "2021-03-24T19:57:13Z")

</div>

I’d say you’ve studied this enough that you can propose that your best function gets added to `Base` =]

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 24, 2021, 8:23pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/16 "2021-03-24T20:23:05Z")

</div>

@gustaphe, it is apparent that you’ve found the right plunger shapes to unclog the non-unique problem. Thanks for the inspirational drawing.

---

<div class="post-metadata">

**Author:** ![Andrei\_Bobrov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrei_bobrov/32/8438_2.png) [@Andrei\_Bobrov](https://discourse.julialang.org/u/Andrei_Bobrov)\
**Post date:** [March 24, 2021, 8:32pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/17 "2021-03-24T20:32:24Z")

</div>

> a = [1,2,3,4,3,5,3,2]  
> [i for i in unique(a) if sum(i .== a) \> 1]

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 24, 2021, 9:00pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/18 "2021-03-24T21:00:00Z")

</div>

This is not really how things work. New methods are added to `Base` only if they are sufficiently basic, I have to say that I think I never needed something as specific as this.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 25, 2021, 6:06am UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/19 "2021-03-25T06:06:22Z")

</div>

It’s funny, that’s probably what I would have written. A fairly intuitive solution.

It’s _by far_ the slowest of the suggested ones. It really stands out.

![results](https://global.discourse-cdn.com/julialang/original/3X/9/d/9db91a0824f3fc7c7d1beeb4014c1e8cdd45371a.png)

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [March 25, 2021, 12:37pm UTC](https://discourse.julialang.org/t/opposite-of-unique/57802/20 "2021-03-25T12:37:22Z")

</div>

When working with datasets I would try dataframe utilities for such kind of vector problems. It seems another good approach in this case (for current versions).

```julia
using DataFrames, BenchmarkTools
nonunique(x) = 
  filter(:nrow => >(1), combine(groupby(DataFrame(x = x), :x), nrow)).x

julia> @btime nonunique([1, 2, 3, 4, 3, 5, 3, 2])
  12.550 μs (202 allocations: 17.28 KiB)
2-element Array{Int64,1}:
 2
 3

```

ps. The result is unsorted.
