[back]

Nub

Implement the function

val nub : ('a * 'b) list -> ('a * 'b) list

so that nub l is the result of removing entries from l until every key is associated with at most one value. In the case that l has multiple entries with the same key, the leftmost one should be kept and the remaining should be removed. The order of the remaining elements should be maintained.

Hint: first implement the function

val remove_key : 'a -> ('a * 'b) list -> ('a * 'b) list

so that remove_key k l is the result of removing all entries of l with the key k.

Examples

let _ = assert (nub [(1, "1"); (2, "2")] = [(1, "1"); (2, "2")])
let _ = assert (nub [(1, "1"); (2, "2"); (1, "3")] = [(1, "1"); (2, "2")])
let _ = assert (nub [(1, "1"); (2, "2"); (1, "3"); (1, "4")] = [(1, "1"); (2, "2")])
let _ = assert (nub [(2, "2"); (1, "3"); (1, "4"); (2, "5")] = [(2, "2"); (1, "3")])