java - Finding duplicate elements in an array without using nested loops -
this question has answer here:
- find duplicate element in array in time o(n) 21 answers
i had interview consisted of following problem. please possible solutions.
write method in java find duplicate elements in integer array without using nested loops ( for/ while / while, etc ) , without using library functions or standard api's.
hey below solution has complexity o(n) , works fine. check if 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]+" "; } } } }
Comments
Post a Comment