
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the first repeating element in an array of integers C++
In this problem, we are an array arr of n integer values. Our task is to find the first repeating element in an array of integers.
We need to find the first integer value from the array which occurred more than once in the array.
Let's take an example to understand the problem,
Input : arr[] = {4, 1, 8, 9, 7, 2, 1, 6, 4} Output : 4
Explanation −
Integers with more than one occurrence are 4 and 1. 4's first occurrence is smaller than 1. Hence the answer is 4
Solution Approach
A simple solution to the problem is using nested loops. We will use two loops, outer one to iterate each integer value of the array and the inner one to check if there is another integer value present in the array with the same value. If yes, return the value. This method is good but in case of no solution, it will have O(N2) complexity.
Solution Approach
Another approach which can solve the problem better is by using hashing. We will traverse the array from the last index and then update the minimum index for the element found at the index which is visited.
Example
Program to illustrate the working of our solution
#include<bits/stdc++.h> using namespace std; int findRepeatingElementArray(int arr[], int n){ int minRetIndex = -1; set<int> visitedElements; for (int i = n - 1; i >= 0; i--){ if (visitedElements.find(arr[i]) != visitedElements.end()) minRetIndex = i; else visitedElements.insert(arr[i]); } if (minRetIndex != -1) return arr[minRetIndex]; else return -1; } int main(){ int arr[] = {4, 1, 6, 3, 4, 1, 5, 8}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The element with repeated occurence is "<<findRepeatingElementArray(arr, n); }
Output
The element with repeated occurence is 4
- Related Articles
- Find the only repeating element in a sorted array of size n using C++
- Write a program in C++ to find the top K frequent element in an array of integers
- Find last element after deleting every second element in array of n integers in C++
- Write a program to find the first non-repeating number in an integer array using Java?
- C# Program to display the first element from an array
- Detecting the first non-repeating string in Array in JavaScript
- First element that appears even number of times in an array in C++
- Find the maximum element in an array which is first increasing and then decreasing in C++\n
- Finding the index of first element in the array in C#
- Product of non-repeating (distinct) elements in an Array in C++
- Finding the first redundant element in an array - JavaScript
- Write a program in C++ to find the most frequent element in a given array of integers
- How to get the first element of an array in PHP?
- Maximum difference between first and last indexes of an element in array in C
- Find first repeating character using JavaScript
