# Return duplicate rows in array with no of times and index of first occurence

**URL:** <https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176>\
**Category:** General Usage\
**Tags:** question, array\
**Created:** [July 13, 2022, 1:48pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176 "2022-07-13T13:48:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 13, 2022, 1:48pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/1 "2022-07-13T13:48:21Z")

</div>

I have a matrix A=[1,2; 2,3; 3,4;1,2],

I wanted to return duplicated rows (here 1,2), no of repetitions (here 1) and the row index of the first occurrence (here 1st row)

I have tried many thing with unique, but nothing worked out

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 13, 2022, 2:01pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/2 "2022-07-13T14:01:33Z")

</div>

One small package for such things is:

```julia
julia> using GroupSlices

julia> A = [1 2; 2 3; 3 4; 1 2.0] # edited
4×2 Matrix{Float64}:
 1.0 2.0
 2.0 3.0
 3.0 4.0
 1.0 2.0

julia> groupslices(A, dims=1)
4-element Vector{Int64}:
 1
 2
 3
 1

julia> groupinds(ans)
3-element Vector{Vector{Int64}}:
 [1, 4]
 [2]
 [3]

julia> filter(is -> length(is)>1, ans)
1-element Vector{Vector{Int64}}:
 [1, 4]

```

---

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 13, 2022, 2:09pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/3 "2022-07-13T14:09:27Z")

</div>

Thanks , is there any way to get the no of repetitions?  
This one gives my the repeated numbers and the row value

“” countmap(collect(eachrow(A))) “”"

---

<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:** [July 13, 2022, 2:28pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/4 "2022-07-13T14:28:38Z")

</div>

Extended example - what number do you want?

```julia
B = [1 2; 2 3; 3 4; 1 2.0; 2 3; 2 3]
y = countmap(collect(eachrow(B)))
sum(x -> x>1 ? x : 0, values(y)) # = 5 : total number of repeated rows
sum(>(1), values(y)) # = 2 : number of distinct rows repeated 

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 13, 2022, 2:44pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/5 "2022-07-13T14:44:59Z")

</div>

```julia

A=[1 2; 2 3; 3 4; 1 2.0;1 2;1 3;3 4]

julia> function norepandfo(A)
           grp=[findall(==(r),eachrow(A)) for r in unique(eachrow(A))]       
           collect(zip(length.(grp).-1, first.(grp)))
       end
norepandfo (generic function with 1 method)

julia> norepandfo(A)
4-element Vector{Tuple{Int64, Int64}}:
 (2, 1)
 (0, 2)
 (1, 3)
 (0, 6)

```

---

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 13, 2022, 2:49pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/6 "2022-07-13T14:49:29Z")

</div>

This worked for me  
Thanks all

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [July 13, 2022, 3:25pm UTC](https://discourse.julialang.org/t/return-duplicate-rows-in-array-with-no-of-times-and-index-of-first-occurence/84176/7 "2022-07-13T15:25:09Z")

</div>

this version of the function returns to a generator that can be collected or filtered:

```julia

function noofrepandfo(A)
    grp=[findall(==(r),eachrow(A)) for r in unique(eachrow(A))]
    zip(length.(grp).-1, first.(grp))
end

[t for t in noofrepandfo(A)]

[t for t in noofrepandfo(A) if first(t)!=0]

```

```julia

julia> [t for t in noofrepandfo(A) if first(t)!=0]
2-element Vector{Tuple{Int64, Int64}}:
 (2, 1)
 (1, 3)

julia> [t for t in noofrepandfo(A)]
4-element Vector{Tuple{Int64, Int64}}:
 (2, 1)
 (0, 2)
 (1, 3)
 (0, 6)

```
