The approach depends on which version you are using. In Excel 365,
UNIQUE(range) extracts a list of distinct values from the range as a dynamic array. COUNTA then counts how many items are in that list. Together they give you the count of distinct values. In Google Sheets, COUNTUNIQUE does the same thing in one step. In older Excel without UNIQUE, the SUMPRODUCT trick works by using
COUNTIF to count how many times each value appears in the range. Dividing 1 by the count gives the reciprocal — a value that appears three times contributes 1/3 three times, which sums to 1. A value appearing once contributes 1 once. SUMPRODUCT adds all these fractions and the total equals the number of unique values. It sounds complex but it reliably returns the right answer.