Hi Friends,
I would like to know what should be the approach to solve questions like these:
1. In an array of X elements, check to see if there are any duplicate values by providing 2 approachs:
a. to maximize-performance (0(n))
b. to minimize-memory-usage
I wrote this test program to find duplicates and now I wonder how can i change my program to follow the above said 2 approaches:
import java.util.*;
public class test{
public static void main(String[] ar)
{
int[] arr = {3,5,6,4,2,3,5,2,4,2};
StringBuffer sb = new StringBuffer();
Arrays.sort(arr);
boolean flag=false;
for(int i=0;i<10;i++)
{
System.out.println(arr);
}
for(int i=0;i<9;i++)
{
if(arr[i]==arr[i+1]){
if(!flag)
sb.append(arr[i]+" ");
flag=true;
continue;
}else{
flag=false;
}
}
System.out.println("Duplicates:"+sb.toString());
}
}
Your help would be greatly appreciated. Thanks in advance.