InterviewBit - Find duplicates in Array  

by ne on 2021-09-29 under Algo/DS/Problems

This problem is a property of InterviewBit (www.interviewbit.com).

Full details of the question, and if you want to try it out yourself, proceed to interviewbit : https://www.interviewbit.com/problems/find-duplicate-in-array/

Here's the implementation:

 


public class Solution {
    
    public int repeatedNumber(final int[] A) {
        int max=Integer.MIN_VALUE;
        for(int i=0;i<A.length;i++){
            if(max<A[i]){
                max=A[i];
            }
        }
        int[] buck=new int[max+1];
        for(int i=0;i<A.length;i++){
            buck[A[i]]++;
            if(buck[A[i]]>1){
                return A[i];
            }
        }
        return -1;
        
    }
}




Happy Coding !