Flag first duplicate in a list
To mark the first duplicate in a list, you can use a formula based on the COUNTIF function. Optionally, you can flag subsequent duplicates with a different marker. In the example shown the formula in cell C4 is:
This formula has been copied down the column, from C4 to C11.
How this formula works
At the core, this formula is composed of two sets of the COUNTIF function wrapped in the IF function. The outer IF + COUNTIF first checks to see if the the value in question (B4) appears more than once in the list:
If not, the outer IF function returns an empty string ("") as a final result. If the value does appear more than once, we run another IF + COUNTIF combo. This one does the work of flagging duplicates:
This part of the formula uses an expanding reference ($B$4:B4) that expands as the formula is copied down the column. (The first B4 in the range is absolute (locked), the second is relative, so it changes as the formula is copied down the list).
Remember that this part of the formula is only executed if the first COUNTIF returned a number greater than 1. So, at each row, the formula checks the count inside the range up to the current row. If the count is 1, we mark the duplicate with "x", since it's the first one we've seen. If it's not 1, we know it must be a subsequent duplicate, and we mark with "xx"
To flag the first duplicate in a list only with a 0 or 1, you can use this stripped down formula, which uses an expanding range and the COUNTIFS function.
This formula will return 1 only when a value has been encountered twice – the first occurrence will return zero:
To flag the second and all subsequent occurrences, the formula in F5 above is:
Note: In both examples, adding zero is just a simple way to coerce TRUE and FALSE values to 1 and 0.
Also, using COUNTIFS instead of COUNTIF makes it possible to evaluate values in other columns as part of the test for duplicates. Each additional column also needs to be entered as an expanding range.