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

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

One small package for such things is:

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]
1 Like

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))) “”"

Extended example - what number do you want?

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 

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)

This worked for me
Thanks all

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


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> [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)