0

I recently had an interview which consisted of following problem. Please help with possible solutions.

Write a method in Java to find duplicate elements in an integer array without using nested loops ( for/ while / do while, etc ) and without using library functions or standard API's.

Dev Choudhary
  • 105
  • 2
  • 8

1 Answers1

1

Hey the below solution has complexity O(n) and works fine. Check if it helps.

public class Main {
    public static void main(String[] args) {
        int a[] = new int[]{10,3,5,10,5,4,6};
        String distinctElement="";
        String repetitiveTerms="";
        for(int i=0;i<a.length;i++){
            if(i==0){
                distinctElement+=a[i]+" ";
            }
            else if(distinctElement.contains(""+a[i])){
                repetitiveTerms+=a[i]+" ";
            }
            else{
                distinctElement+=a[i]+" ";
            }
        }

    }
}
Kulbhushan Singh
  • 627
  • 4
  • 20