Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Finding duplicates in Array

843785Jul 3 2008 — edited Jul 3 2008
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 31 2008
Added on Jul 3 2008
1 comment
351 views