ORACLE/AMAZON

 Find the first repeating element in an array of integers

Given an array of integers, find the first repeating element in it. We need to find the element that occurs more than once and whose index of first occurrence is smallest. 


SOLUTION:


#include <iostream>

#include <bits/stdc++.h>

#include <climits>


using namespace std;




void display(int *arr, int n){


    for(int i=0;i<n;i++){


        cout<<arr[i];


    }


}






int main() {


    int arr[]={1,100,30,30,30,100,1};


    int n=sizeof(arr)/sizeof(int);


    //cout<<n;


    int max=INT_MIN;

    int count=0;

    //int sum=0;

    for (int i=0;i<n;i++){

        for(int j=i+1;j<n;j++){

            if(arr[i]==arr[j]){

                cout<<i+1<<endl;

                count++;

                break;

            }

        if (count==1){

            break;

        }

        }

        if (count==1){

            break;

        }

        

            

            

        

        

    }

    


    

   // cout<<count;

    //cout<<ans;


    //cout<<sum;


    //display (arr,n);


    




    return 0;


}

Input:  arr[] = {10, 5, 3, 4, 3, 5, 6}
Output: 5 [5 is the first element that repeats]

Input:  arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10}
Output: 6 [6 is the first element that repeats]
zscscasd



Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings