Попробуйте использовать логический массив, чтобы указать, существует значение или нет. Используйте true
, чтобы указать, что номер существует.
int main(void)
{
const unsigned int array_values[10] = {1, 2, 3, 3, 4, 5, 6, 7, 8, 9};
bool is_resident[10] = {false};
bool duplicate = false;
unsigned int index = 0;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
duplicate = duplicate || is_resident[array[index]];
is_resident[array[index++]] = true;
std::cout << "Duplicate found: " << (char *)(duplicate ? "true" : "false") << std::endl;
return 0;
}
Приведенный выше код не проверен и не специализирован для этого вопроса. Этот код не имеет других целей в жизни и не обобщается.
Примечание. В этом примере петли не использовались и не наносились.
«Всегда существует третий вариант». - Томас Мэтьюз