How to count distinct values in a list in linear time?
问题 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? 回答1: Update: - distinct vs. unique If you are looking for "unique" values (As in if you see an element "JASON" more than once, than it is no longer unique and should not be counted) You can do that in linear time by using a HashMap ;) (The generalized / language-agnostic idea is Hash table) Each entry of a HashMap / Hash table is <KEY, VALUE