C C++ JAVA PYTHON SQL HTML CSS DSA Robotics AWS CODING INTERVIEW PREPARATION

Made with  & Code

# Program to find the first repeating element in an array of integers in C++

Written By -

## C++ program to find the first repeating element in an array of integers

Example:

Input: {5, 15, 20, 5, 6, 10, 15, 10}

Output: 5

### # Algorithm

1. Take array as input.
2. Run two loops, the first loop selects every element from the array and second loop traverse ahead and check for the duplicates.
3. If duplicate is found, print the first repeating integer else print no integer repeated.

Code:

``````#include <bits/stdc++.h>
using namespace std;

int main(){
int array, n, i;
cout << "Enter number of elements: ";
cin >> n;
cout << "\nEnter elements: ";
for (i = 0; i < n; i++)
cin >> array[i];

cout << "Original array: ";

for (int i = 0; i < n; i++)
cout << array[i] << " ";

// selecting an element

for (int i = 0; i < n; i++)
//traversing to check repetition

for (int j = i + 1; j < n; j++)
if (array[i] == array[j]){
cout << "\nFirst repeating integer is " << array[i];
return 0;
}

cout << "No integer repeated\n";
return 0;
}``````

Time Complexity: O(N2)

Related Posts:

Online Compilers