Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

find duplicate from an array c++

// Find duplicate array elements from an array
// Author: Subodh Chandra 

    void findDuplicateWay02(int arr[], int size)
    {
        //! Optimized solution
        //* Time complexity: O(n)
        takeInput(arr, size); // for taking array element input
        sort(arr, arr + size); // sort the array
        cout << "Your array is: ";
        display(arr, size); // demonstrating the array 

        vector<int> v;
        for (int i = 0, count = 0; i < size; i += count)
        {
            count = 0;
            if (arr[i] == arr[i + 1])
            {
                v.push_back(arr[i]);
                count += 2;
            }

            else if (arr[i] != arr[i + 1])
                count *= 0 + 1;
        }

        cout << endl
             << "Duplicate array elements are: ";
        for (int i : v)
        {
            cout << i << " ";
        }
    }
 
PREVIOUS NEXT
Tagged: #find #duplicate #array
ADD COMMENT
Topic
Name
7+8 =