How many values are common in 2 lists? [homework]

Here is a formula challenge for you.

How many values are common in 2 lists [homework]

Lets say we have 2 lists of values in A1:A10 & B1:B10

Now, how do you find the number of common values in both lists?

We just want the count, not list of common values it self.

Go ahead and figure out the formula and post your answers here.

Bonus challenges:

  1. Write the formula if 2 lists have non-numeric values (text for example) ?
  2. Write the formula if 2 lists are not of same size (A1:A10 and B1:B15 for example) ?
  3. Write the formula if we have 3 lists (all same size, numeric values) ?

Go ahead and share your answers. I am eager to see what you will come up with.

PS: For some inspiration, check out this forum discussion.

PPS: If you just want to highlight the common values, see this.

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

78 Responses

  1. I guess this would make it : {=SUM(COUNTIF(A3:A12;”=”&B3:B12))}

    It works with both numeric and text values and range B3:B12 can be extended to a third row.

    Note: My excel is in french so I tried to translate functions to english. French formula is  {=SOMME(NB.SI(A3:A12;”=”&B3:B12))}

  2. If you have your data in Columns A, B and C, put a helper column in, and sum over it:
     `=IF(AND(ISNUMBER(MATCH(A2,$B:$B,FALSE)),ISNUMBER(MATCH(A2,$C:$C,FALSE))),1,0)`

    It doesn’t matter if you compare to all elements in A, B or C, as if it has to be common over all three then it must appear in that list (that’s why you only need two statements in your AND() formula) – this can be expanded for as many sets of data as you want, and it doesn’t matter about the relative size, as long as you make your helper column as long as column A.  If you were doing this for large sets of data then you’d probably want to replace A with whichever was shortest, but in the example given it doesn’t really matter.

     If you wanted to find out if it’s common in any two of the three rows it gets a little more complicated, but you still use the same basic idea:

    ‘=IF(OR(ISNUMBER(MATCH(A2,B:B,FALSE)),ISNUMBER(MATCH(A2,C:C,FALSE)),ISNUMBER(MATCH(B2,C:C,FALSE))),1,0)’

    In this case, you’re doing the same basic operation as above, but by replacing the AND() statement with an OR(), and adding in a condition to say “check B against C”. This time you have to make your helper column as long as the second shortest of A, B and C.

    When it comes to conditional formatting, you’d use the following:

    ‘=IF(AND(ISNUMBER(MATCH(A2,B:B,FALSE)),ISNUMBER(MATCH(A2,C:C,FALSE))),TRUE,FALSE)’
    To highlight if it’s common over all three (in A), juggle about for B and C,
    and
    ‘=IF(OR(ISNUMBER(MATCH(A2,B:B,FALSE)),ISNUMBER(MATCH(A2,C:C,FALSE))),TRUE,FALSE)’
    to highlight if it’s common over A and B or A and C.

    Hopefully the explanations make sense, and the formalae work as they look like they do to me,

    George 

  3. = SUMPRODUCT( –NOT(ISERROR(MATCH(ListA; ListB; 0))) )
    = SUMPRODUCT( –NOT(ISERROR( MATCH(ListA; ListB; 0) * MATCH(ListA; ListC; 0) )) )

    And the lists can be of different size.

    Congratulations for your excellent blog, Chandoo. I learn (and enjoy) a lot reading it. I must confess, however, that I am in Daniel Ferry’s Excel Hero Academy 3, which has inspired this solution to your challenge…

    Unai

  4. =sum(–not(isna(match(ListA, ListB, 0))))
     
    I don’t think it matters if the lists are different sizes – I used ISNA instead of ISERROR so that it only caught the missing matches, and not any other errors.
     

  5. Assuming that first value of List A is in A1, first value of list B is in B1 and first value of list C is in C1, and in order to avoid repeated values in any list, we can use:

    {=SUM(IF(COUNT.IF(ListA,ListB)>0,1,0)*IF(COUNT.IF(ListC,ListB)>0,1,0))}

    Defining
    ListA=OFFSET($A$1;;;COUNTA($A:$A);1)
    ListB=OFFSET($B$1;;;COUNTA($B:$B);1)
    ListC=OFFSET($C$1;;;COUNTA($C:$C);1)

    1. Sorry, but in Spanish excel we use semicolon instead comma.

      ListA=OFFSET($A$1,,,COUNTA($A:$A),1)
      ListB=OFFSET($B$1,,,COUNTA($B:$B),1)
      ListC=OFFSET($C$1,,,COUNTA($C:$C),1)

      1. Good morning,
        Interesting formula if E9:E17 contains no duplicate and F9:F17 does. It still gives of number of distinct occurrences.

  6. =COUNTIF($B$2:$B$11,A2)+COUNTIF($B$2:$B$11,A3)+COUNTIF($B$2:$B$11,A4)+COUNTIF($B$2:$B$11,A5)+COUNTIF($B$2:$B$11,A6)+COUNTIF($B$2:$B$11,A7)+COUNTIF($B$2:$B$11,A8)+COUNTIF($B$2:$B$11,A9)+COUNTIF($B$2:$B$11,A10)++COUNTIF($B$2:$B$11,A11)

  7. I will say
    =SUM(IF(Uniques=1,0,1/(Uniques))) (CSE)
    where the named formula “Uniques” is:
    “=COUNTIF(Sheet1!$A$1:$B$10,Sheet1!$A$1:$B$10)” 
    The name is not necessary, just for presentation purposes.
    This method should cover bonus challenges 1 and 3 as well.

  8. Hi
    =SUMPRODUCT(COUNTIF(ListA,ListB))
    Same formula works for bonus questions 1 and 2.
     
    Bonus question 3:
    =SUMPRODUCT(–(COUNTIF(A1:C10,A1:C10)=3))/3
     

  9. For 2 lists, same size: =SUMPRODUCT(COUNTIF(A1:A11,B1:B11))
    or =SUM(COUNTIF(A2:A11,B2:B11)) (array formula)
    or =SUM(IF(COUNTIF(A1:A11,B1:B11)>0,1,0)) (array formula)

    For 2 lists, different sizes: =SUM(COUNTIF(H2:H11,I2:I16)) (array formula)

    For 3 lists, same size: =SUM(IF(COUNTIF(B16:C25,A16:A25)>1,1,0)) (array formula)

  10. It is considered, in the COLUMN A or COLUMN B duplicate value may be typed.
    At first we have find out distinct value in both columns

    =SUMPRODUCT(COUNTIF($A$1:$A$10,B1:B10)*COUNTIF($B$1:$B$10,A1:A10))

  11. 1) Number of common values in both lists(List A and List B):[List A and List B are headers…data starts from A2 and B2 , respectively.]
    =SUMPRODUCT(COUNTIF(B2:B11,A2:A11))    (ctrl+shift+enter)
    2) Formula if 2 lists have non-numeric values (text for example)
    Same formula works in this case as well
    =SUMPRODUCT(COUNTIF(B2:B11,A2:A11))    (ctrl+shift+enter)
    3) 2 lists are not of same size (A1:A10 and B1:B15 for example)
    Same above formula works but only the reference needs to be changed:
    SUMPRODUCT(COUNTIF(B2:B15,A2:A11))    (ctrl+shift+enter)
    4) Formula if we have 3 lists (all same size, numeric values)
    Along with your list A and List B data , say List C contains: 50,12,69,25,60,30,10,58,21,47 (from C2 to C11):
    Formula would be:
    SUMPRODUCT(COUNTIF(B2:B11,A2:A11))+SUMPRODUCT(COUNTIF(C2:C11,A2:A11))
    (ctrl+shift+enter)
    should return 9
    Regards,
    Kaushik

    1. Kaushik,
      About your 4)
      The answer shouldn’t be 9 with the data you provided but 3. The common elements between all tables are 30,50,60.
      On top of this, why do you use SUMPRODUCT when you have only one table e.g. COUNTIF(B2:B11,A2:A11) (cse)? a simple SUM would suffice.

  12. List A

     

    List B

     

    10

     

    11

     

    20

     

    20

     

    30

     

    30

     

    40

     

    33

     

    50

     

    40

     

    60

     

    45

     

    70

     

    50

     

    80

     

    60

     

    90

     

    78

     

    100

     

    99

     

    =IF(COUNTIF($B$2:$B$11,A2),A2,0)

  13. 10
    11
    =IFERROR(VLOOKUP(C2,$D$2:$D$11,1,FALSE),0)
    0

    20
    20
    =IFERROR(VLOOKUP(C3,$D$2:$D$11,1,FALSE),0)
    20

    30
    30
    =IFERROR(VLOOKUP(C4,$D$2:$D$11,1,FALSE),0)
    30

    40
    33
    =IFERROR(VLOOKUP(C5,$D$2:$D$11,1,FALSE),0)
    40

    50
    40
    =IFERROR(VLOOKUP(C6,$D$2:$D$11,1,FALSE),0)
    50

    60
    45
    =IFERROR(VLOOKUP(C7,$D$2:$D$11,1,FALSE),0)
    60

    70
    50
    =IFERROR(VLOOKUP(C8,$D$2:$D$11,1,FALSE),0)
    0

    80
    60
    =IFERROR(VLOOKUP(C9,$D$2:$D$11,1,FALSE),0)
    0

    90
    78
    =IFERROR(VLOOKUP(C10,$D$2:$D$11,1,FALSE),0)
    0

    100
    99
    =IFERROR(VLOOKUP(C11,$D$2:$D$11,1,FALSE),0)
    0

     
     
    =COUNTIF(E2:E11,”>0″)
    5

  14. My answer, assuming there is no duplicate in either lists:
    1°) Write the formula if 2 lists have non-numeric values (text for example) ?
    SUM(COUNTIF(LIST_B,LIST_A)) (cse) or SUM(COUNTIF(LIST_A,LIST_B)) (cse)
    2°) Write the formula if 2 lists are not of same size (A1:A10 and B1:B15 for example) ?
    same as above
    3°) Write the formula if we have 3 lists (NOT all same size, NON-numeric values) ?
    I have 3 lists of different sizes LIST_A; LIST_B; LIST_C containing non-numeric values:
    SUMPRODUCT(COUNTIF(LIST_B,LIST_A),COUNTIF(LIST_D,LIST_A)) (cse)

    1. Apologies.
      In 3°) please read : SUMPRODUCT(COUNTIF(LIST_B,LIST_A),COUNTIF(LIST_C,LIST_A)) (cse)
      (LISt_D replaced by LIST_C)

  15. array formula for two lists (either numeric or text values):
    =SUM(–(($A$1:$A$10)=TRANSPOSE($B$1:$B$15))) 

    and for three lists:
    =SUM((1-ISERROR(MATCH($A$1:$A$10,TRANSPOSE($B$1:$B$11),0)))*(1-ISERROR(MATCH($A$1:$A$10,TRANSPOSE($C$1:$C$8),0))))

    Both formulas should work if lists do not have duplicates
     

  16. BTW: easy way to “translate” excel formulas between different language versions is:
    – select a cell with formula
    – open VBA Editor
    – open an Immediate Window
    – type: ?activecell.Formula
    – and press Enter

    And if you have local (not english) version of excel – again 

    – select a cell 
    – open VBA Editor
    – open an Immediate Window
    – type: activecell.Formula = ” copy english formula
    – and press Enter

  17. I have a single formula which can be used in all these cases :

    1. ” =SUMPRODUCT(COUNTIF(A1:A10,B1:B10)) ”
                   Works with both Numeric and Text Value.

    2. ” =SUMPRODUCT(COUNTIF(A1:A10,B1:B15)) ”
                    Just a version of previous formula with changed range.

    3. “=SUMPRODUCT(COUNTIF(A1:A10,C1:C10),COUNTIF(B1:B10,C1:C10))”
                    Extended version of the First Formula, does well with any no. of columns and range.

    🙂

    Rgds,
    Akash Khandelwal 

  18. This formula array worked, but I don’t really know why the division by 2. Maybe it’s a case for some Forensics?

    {=SUM((COUNTIF(A1:B10,A1:B10)>1)*1)/2}

  19. Simplest Way is select the 2 column of list a and list b go to conditional formatting and use duplicate values it will highlight u duplicate values which is in both the columns ..”no formula required”….

  20. I have to use conditional formatting.
    step1:I have to select total data wher we want change colour.
    step2:then i have to go in conditional formating & select new rule .
    step3: select Use a formuleto determine which cells to formate.
    step4:Type formule =countif(select unique number column,first cell of selected  data)=1
    step5: fill format as you like

  21. {=COUNTIF(ISERROR(INDEX($A$1:$B$11;MATCH(A1:A11;$B$1:$B$11;0);2));0)}

    Did this array formula in Libre Office Calc. (Uses semicolons as variable separators.)

  22. Place the following in a helper column (E) after the three lists are in columns B,C&D:

    =IF(IFERROR(MATCH(B13,$C$3:$C$5,0),””)=””,””,IFERROR(MATCH(B13,$D$12:$D$14,0),””))

    Then just do a COUNT of column E

  23. I created a parallel column C, applied the following formula in C1 and dragged it till C10:

    =–AND(COUNTIF($A$1:A1,A1)=1,COUNTIF($B$1:$B$10,A1)>0)

    Sum of ‘1s’ in C1:C10 is the no. of values that are common between A1:A10 and B1:B10.

  24. This works for varying sizes and also only counts unique occurrences:

    {=SUM(–(FREQUENCY(IF(ISNA(MATCH(B2:B20,C2:C20,0)),0,MATCH(B2:B20,C2:C20,0)),IF(ISNA(MATCH(B2:B20,C2:C20,0)),0,MATCH(B2:B20,C2:C20,0)))>0))}

  25. Q1-Q3:
    ={SUMPRODUCT(COUNTIF(A2:An,B2:Bm))}
    works with different list sizes.

    Q4:
    ={SUMPRODUCT(COUNTIF(A2:An,C2:Cn),COUNTIF(B2:Bn,C2:Cn))}

    Criteria range must be same in both arguments. I tested permuting A, B, C. If criteria range is different it won’t work.

  26. This allows numeric/text mixed of different lengths and yields the innerjoin count:

    =SUMPRODUCT(ISNUMBER(MATCH(B1:B11,D1:D15,0))*1)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.