Remove Duplicate Elements from an array

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
*
*/

/**
* @author ashish
*
*/
public class NoDuplicateArray {

/**
* @param args
*/
public static void main(String[] args) {
removeduplicate();
}

public static void removeduplicate() {
int a[] = { 5, 5,5,5,7, 8, 9, 9 };
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length-1; i++) {

if (a[i] == a[i+1])
{

list.add(a[i]);
i++;
}

else
{
list.add(a[i]);
}

}

list.trimToSize();
System.out.println(“Size of the list is”+list.size());
System.out.println(“Contents of the array are “);
for(int i=1;i<list.size();i++)
{
if(list.get(i-1)==list.get(i))
{
list.remove(i);
}
}

for(int j:list)
System.out.println(j);
}

}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s