Solution to the Distinct problem in C++