I can think of sorting them and then going over each element one by one but this is nlogn. Is there a linear method to count distinct elements in a list?
Add every element of the list to a HashSet and then check the size (cardinality) of the HashSet, which is the number of distinct values in the list.