Given an array arr[], find the first repeating element index. The element should occur more than once and the index of its first occurrence should be the smallest.
Note:- The position you return should be according to 1-based indexing.
Examples:
Input: arr[] = [10, 5, 3, 4, 3, 5, 6]
Output: 5
Explanation: 5 is the first element that repeatsInput: arr[] = [6, 10, 5, 4, 9, 120, 4, 6, 10]
Output: 6
Explanation: 6 is the first element that repeats
Table of Content
[Naive Approach] - Using Nested Loops - O(n^2) Time and O(1) Space
Run two nested loops, the outer loop picks an element one by one, and the inner loop checks whether the element is repeated or not. Once a repeating element is found, break the loops and return the element.
#include <iostream>
using namespace std;
int firstRepeated(vector<int> &arr) {
// Nested loop to check for repeating elements
int n = arr.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
return i + 1;
}
}
}
// If no repeating element is found, return -1
return -1;
}
int main() {
vector<int>arr = {10, 5, 3, 4, 3, 5, 6};
int index = firstRepeated(arr);
cout << index << endl;
return 0;
}
public class GFG {
public static int firstRepeated(int[] arr) {
int n = arr.length;
// Nested loop to check for repeating elements
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
return i + 1;
}
}
}
// If no repeating element is found, return -1
return -1;
}
public static void main(String[] args) {
int[] arr = {10, 5, 3, 4, 3, 5, 6};
int index = firstRepeated(arr);
System.out.println(index);
}
}
def firstRepeated(arr):
n = len(arr)
# Nested loop to check for repeating elements
for i in range(n):
for j in range(i + 1, n):
if arr[i] == arr[j]:
return i + 1
# If no repeating element is found, return -1
return -1
if __name__ == '__main__':
arr = [10, 5, 3, 4, 3, 5, 6]
index = firstRepeated(arr)
print(index)
using System;
public class GFG {
public static int firstRepeated(int[] arr) {
int n = arr.Length;
// Nested loop to check for repeating elements
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
return i + 1;
}
}
}
// If no repeating element is found, return -1
return -1;
}
public static void Main(String[] args) {
int[] arr = {10, 5, 3, 4, 3, 5, 6};
int index = firstRepeated(arr);
Console.WriteLine(index);
}
}
function firstRepeated(arr) {
let n = arr.length;
// Nested loop to check for repeating elements
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (arr[i] === arr[j]) {
return i + 1;
}
}
}
// If no repeating element is found, return -1
return -1;
}
// Driver code
let arr = [10, 5, 3, 4, 3, 5, 6];
let index = firstRepeated(arr);
console.log(index);
Output
2
[Expected Approach] - Hash Map - O(n) Time and O(n) Space
Store the frequency of each element. Then traverse the array from left to right and return the 1-based index of the first element whose frequency is greater than 1.
- Traverse the array and store the frequency of each element in a hash map.
- Traverse the array again from left to right.
- If the frequency of the current element is greater than 1, return its 1-based index.
- If no repeating element is found, return -1.
Let us understand with an example:
Consider arr[] = [10, 5, 3, 4, 3, 5, 6]
- Store frequencies: {10:1, 5:2, 3:2, 4:1, 6:1}.
- Check index 1 (element 10): frequency is 1, so continue.
- Check index 2 (element 5): frequency is 2, so return 2.
- Output: 2
#include <iostream>
#include<vector>
using namespace std;
int firstRepeated(vector<int> &arr) {
int ans = -1;
// using map to store frequency of each element.
std::unordered_map<int, int> m;
// storing the frequency of each element in map.
for (int i = 0; i < arr.size(); i++)
m[arr[i]]++;
// iterating over the array elements.
for (int i = 0; i < arr.size(); i++) {
// if frequency of current element in map is greater than 1,
// then we store the index and break the loop.
if (m[arr[i]] > 1) {
ans = i + 1;
break;
}
}
// returning the position of the first repeating element.
return ans;
}
int main()
{
vector<int> arr = {10, 5, 3, 4, 3, 5, 6};
int index = firstRepeated(arr);
cout<< index<< endl;
return 0;
}
import java.util.HashMap;
public class GFG {
public static int firstRepeated(int[] arr) {
int ans = -1;
// using map to store frequency of each element.
HashMap<Integer, Integer> m = new HashMap<>();
// storing the frequency of each element in map.
for (int i = 0; i < arr.length; i++)
m.put(arr[i], m.getOrDefault(arr[i], 0) + 1);
// iterating over the array elements.
for (int i = 0; i < arr.length; i++) {
// if frequency of current element in map is greater than 1,
// then we store the index and break the loop.
if (m.get(arr[i]) > 1) {
ans = i + 1;
break;
}
}
// returning the position of the first repeating element.
return ans;
}
public static void main(String[] args) {
int[] arr = {10, 5, 3, 4, 3, 5, 6};
int index = firstRepeated(arr);
System.out.println(index);
}
}
def firstRepeated(arr):
ans = -1
# using map to store frequency of each element.
m = {}
# storing the frequency of each element in map.
for i in range(len(arr)):
if arr[i] in m:
m[arr[i]] += 1
else:
m[arr[i]] = 1
# iterating over the array elements.
for i in range(len(arr)):
# if frequency of current element in map is greater than 1,
# then we store the index and break the loop.
if m[arr[i]] > 1:
ans = i + 1
break
# returning the position of the first repeating element.
return ans
if __name__ == '__main__':
arr = [10, 5, 3, 4, 3, 5, 6]
index = firstRepeated(arr)
print(index)
using System;
using System.Collections.Generic;
public class GFG
{
public static int firstRepeated(int[] arr)
{
int ans = -1;
// using map to store frequency of each element.
Dictionary<int, int> m = new Dictionary<int, int>();
// storing the frequency of each element in map.
for (int i = 0; i < arr.Length; i++)
{
if (m.ContainsKey(arr[i]))
m[arr[i]]++;
else
m[arr[i]] = 1;
}
// iterating over the array elements.
for (int i = 0; i < arr.Length; i++)
{
// if frequency of current element in map is greater than 1,
// then we store the index and break the loop.
if (m[arr[i]] > 1)
{
ans = i + 1;
break;
}
}
// returning the position of the first repeating element.
return ans;
}
public static void Main()
{
int[] arr = {10, 5, 3, 4, 3, 5, 6};
int index = firstRepeated(arr);
Console.WriteLine(index);
}
}
function firstRepeated(arr) {
let ans = -1;
// using map to store frequency of each element.
let m = new Map();
// storing the frequency of each element in map.
for (let i = 0; i < arr.length; i++)
m.set(arr[i], (m.get(arr[i]) || 0) + 1);
// iterating over the array elements.
for (let i = 0; i < arr.length; i++) {
// if frequency of current element in map is greater than 1,
// then we store the index and break the loop.
if (m.get(arr[i]) > 1) {
ans = i + 1;
break;
}
}
// returning the position of the first repeating element.
return ans;
}
// Driver code
let arr = [10, 5, 3, 4, 3, 5, 6];
let index = firstRepeated(arr);
console.log(index);
Output
2