I wrote the C++ code below to find the highest missing positive integer in a vector. However, it doesn't work if there are duplicate values in the vector. Could anybody suggest an efficient method to remove duplicates without sorting the vector as the logic depends on the order of the vector being maintained.
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
FirstMissingPositive(std::vector<int> a) { //size int N = a.size(); // a dummy value to replace // integers below 0 and above N int FLAG = N + 2; for (int i = 0; i < N; ++i) { if (a[i] <= 0 || a[i] > N) { a[i] = FLAG; } } // Formula loop for (int i = 0; i < N; ++i) { if (a[i] == FLAG || a[i] == -FLAG) { continue; } int value = abs(a[i]); a[value - 1] = -abs(a[value -1]); } return N + 1; }