I would like to return first (first met, from left to right) non repeating element in an array.
i come with an algorithm that return smallest integer that is non repeating in an array quite easily, using only a array as extra space with length the max integer value in the array:
// smallest non repeating integer
int firstNonRepeatingInteger(int* input, int n)
{
max = std::numeric_limits<int>::min() ;
for(int i = 0 ; i < n ; i++)
{
if(input[i] > max)
max = input[i];
}
int* count = new int[max];
for(int i = 0 ; i < n ; i++)
count[input[i]] += 1 ;
int j = 0;
while(count[i] != 1)
j++ ;
if(j < n)
return input[count[j]] ;
else
return -1 ;
}
however, i cannot find an algorithm to find the first met, except having another n-array storing the time an integer is encountered.
any idea ? any other implementation of first algorithm?
thanks
std::find_if_notand comparing with the first element would work.find_if_not, if you can use it, here's a sample.