Более элегантный способ проверки дубликатов в массиве C ++?

Я написал этот код на C ++ как часть задачи uni, где мне нужно убедиться, что в массиве нет дубликатов:

// Check for duplicate numbers in user inputted data
    int i; // Need to declare i here so that it can be accessed by the 'inner' loop that starts on line 21
    for(i = 0;i < 6; i++) { // Check each other number in the array
        for(int j = i; j < 6; j++) { // Check the rest of the numbers
            if(j != i) { // Makes sure don't check number against itself
                if(userNumbers[i] == userNumbers[j]) {
                    b = true;
                }
            }
            if(b == true) { // If there is a duplicate, change that particular number
                cout << "Please re-enter number " << i + 1 << ". Duplicate numbers are not allowed:" << endl;
                cin >> userNumbers[i];
            }
        } // Comparison loop
        b = false; // Reset the boolean after each number entered has been checked
    } // Main check loop

Он работает отлично, но я бы хотел узнайте, есть ли более элегантный или эффективный способ проверки.

16
задан Bill the Lizard 4 February 2013 в 18:54
поделиться