The STL is often not an option in such interview questions, but here's one way to do #1 using the STL, although it does incur an additional sort (as explained by Terry's answer):
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 2, 2, 3, 2, 1, 4, 1, 3, 4, 1 };
int * end = a + sizeof(a) / sizeof(a[0]);
std::sort(a, end); // O(n log n)
end = std::unique(a, end); // O(n)
std::copy(a, end, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
Here's the result:
$ ./a.out
1 2 3 4
std::unique()
is generally implemented using the same technique Terry described in his answer (see bits/stl_algo.h
in g++'s STL implementation for an example of how to implement it).