Forum Discussion

brunomerola's avatar
brunomerola
Brass Contributor
Nov 19, 2022

Count exact matches in multiple columns

What would be the most efficient way using dynamic arrays or lambda to count the numbers of exact matches in each row of a dataset?

 

Expected outcome:

 

#1#2#3#4# of matches
AAA 3 (#1 and #2, #1 and #3, #2 and #3)
AAB 1
ABA 1
ABC 0
AAAA6
AABB2
ABBC1
ABCD0

 

4 Replies

  • dscheikey's avatar
    dscheikey
    Bronze Contributor

    brunomerola 

    If your real data does not have very many columns, then you can stay simple and use the following formula:

    =SUM(COUNTIF(B1:D1,A1),COUNTIF(C1:D1,B1),COUNTIF(D1:D1,C1))

    Then copy it down.

    • brunomerola's avatar
      brunomerola
      Brass Contributor
      Thank you, it works perfectly, but I'll have dozens of columns and hundreds of rows.
      How would this be converted to a one single array formula?
      • brunomerola's avatar
        brunomerola
        Brass Contributor

        With a little help from combinatorics, I think I found it:

         

        =BYROW(array,LAMBDA(row,
            LET(
                unique,UNIQUE(TOROW(row,1,1),1),
                freq,COUNTIF(row,unique),
                SUM(IF(freq>1,COMBIN(freq,2)))
            )
        ))

         

Resources