Search
 
SCRIPT & CODE EXAMPLE
 

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 << " ";
        }
    }
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to input a vector when size is unknown 
Cpp :: swap elements array c++ 
Cpp :: built in function in c++ for binary to decimal 
Cpp :: c++ rand include 
Cpp :: draw rectangle opencv c++ 
Cpp :: rand() c++ 
Cpp :: c++ program to print natural numbers from 1 to 10 in reverse order using while loop 
Cpp :: how to declare a 2D vector in c++ of size m*n with value 0 
Cpp :: cpp absolute value 
Cpp :: reverse function in cpp array 
Cpp :: function in struct c++ 
Cpp :: Bresenham line drawing opengl cpp 
Cpp :: argument vs parameter coding c++ 
Cpp :: a square plus b square plus c square 
Cpp :: c ifdef 
Cpp :: c++ squaroot 
Cpp :: how can we create 4 digit random number in c++ 
Cpp :: array length c++ 
Cpp :: How do I read computer current time in c++ 
Cpp :: linked list in c++ 
Cpp :: attention nlp 
Cpp :: vectors c++ 
Cpp :: Nested if...else 
Cpp :: lambda function in c++ 
Cpp :: online ide c++ 
Cpp :: print hola mundo 
Cpp :: how to access a vector member by its index 
Cpp :: hierarchical inheritance in c++ employee 
Cpp :: vector::at() || Finding element with given position using vector in C++ 
Cpp :: variadic template in c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =