Skip to main content

Posts

Showing posts with the label First Repeating Element

First Repeating Element

Task: Find the first repeating element in given an array of N elements. If it is present then print that index else print -1. Example: Input: 1 1 2 2 3 1 Output: 1  Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n];     unordered_map<int, int> m;     for(int i=0;i<n;i++)     {         cin>>a[i];         m[a[i]]++;     }     int c=0;     for(int i=0;i<n;i++)     {         if(m[a[i]]>1)         {             cout<<i+1<<endl;             c=1;             break;         }     }     if(c==0) cout<<-1<<endl; } return 0; } Here is the Video: