InterviewBit - Find duplicates in Array  

by ne on 2020-12-02 under Algo

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 !