Find unique numbers in array

后端 未结 8 1141
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 20:14

Well, I have to find how many different numbers are in an array.

For example if array is: 1 9 4 5 8 3 1 3 5

The output should be 6, because 1,9,4,5,8,3 are uniqu

相关标签:
8条回答
  • 2021-02-04 20:46

    Let me join the party ;)

    You could also use a hash-table:

    #include <unordered_set>
    #include <iostream>
    
    int main() {
    
        int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
        const size_t len = sizeof(a) / sizeof(a[0]);
    
        std::unordered_set<int> s(a, a + len);
    
        std::cout << s.size() << std::endl;
        return EXIT_SUCCESS;
    
    }
    

    Not that it matters here, but this will likely have the best performance for large arrays.


    If the difference between smallest and greatest element is reasonably small, then you could do something even faster:

    • Create a vector<bool> that spans the range between min and max element (if you knew the array elements at compile-time, I'd suggest the std::bitset instead, but then you could just compute everything in the compile-time using template meta-programming anyway).
    • For each element of the input array, set the corresponding flag in vector<bool>.
    • Once you are done, simply count the number of trues in the vector<bool>.
    0 讨论(0)
  • 2021-02-04 20:50

    Since you've stated that you cannot use the standard library and must use loops, let's try this solution instead.

    #include <iostream>
    
    using namespace std; // you're a bad, bad boy!
    
    int main() 
    {
        int r = 0, a[50], n;
    
        cout << "How many numbers will you input? ";
        cin >> n;
    
        if(n <= 0)
        {
            cout << "What? Put me in Coach. I'm ready! I can do this!" << endl;
            return -1;
        }
    
        if(n > 50)
        {
            cout << "So many numbers! I... can't do this Coach!" << endl;
            return -1;
        }   
    
        cout << "OK... Enter your numbers now." << endl;
    
        for (int i = 0; i < n; i++)
            cin >> a[i];
    
    
        cout << "Let's see... ";
    
        // We could sort the list but that's a bit too much. We will choose the
        // naive approach which is O(n^2), but that's OK. We're still learning!
    
        for (int i = 0; i != n; i++) 
        { // Go through the list once.      
            for (int j = 0; j != i; j++)
            { // And check if this number has already appeared in the list:
                if((i != j) && (a[j] == a[i]))
                { // A duplicate number!        
                    r++; 
                    break;
                }
            }
        }
    
        cout << "I count " << n - r << " unique numbers!" << endl;
    
        return 0;
    }
    

    I urge you to not submit this code as your homework - at least not without understanding it. You will only do yourself a disservice, and chances are that your instructor will know that you didn't write it anyways: I've been a grader before, and it's fairly obvious when someone's code quality magically improves.

    0 讨论(0)
提交回复
热议问题