У вас есть массив, поэтому вам нужно только пройти через него и проверить, совпадает ли текущее число с любым из чисел в массиве; если да, вернуть true при первом совпадении и пропустить число.
Ниже приведенопример кода:
// pass the array and the number you are checking for existence
int isRepeating(int *array, int unique)
{
int i, l = sizeof(array) / sizeof(int); // find size of the array
// loop thru the array and match any value
for (i = 0; i < l; i++)
{
// if matches, return positive
if (a[i] == unique) return true;
}
// otherwise return negative
return false;
}
int main(int argc, char *argv[])
{
// out array of existing numbers
int array[5] = {1, 2, 3, 4, 5};
// the number we want to insert
int nextOne = 3;
// we check it its already in existence, if so, take appropriate actions
if (isRepeating(array, nextOne)) {
std::cout << "Oops, number " << nextOne << " is already in existence." << std::endl;
}
// your logic here
return 0;
}
ps Мне очень нравится решение set ().