Find/Removed duplicate value in array ?

package testapp;

import java.util.ArrayList;

public class FindDuplicate {

public static void main(String[] args) {

ArrayList arrList= new ArrayList();
arrList.add(“1”);
arrList.add(“6”);
arrList.add(“2”);
arrList.add(“3”);
arrList.add(“3”);
arrList.add(“4”);
arrList.add(“5”);
arrList.add(“6”);
arrList.add(“3”);
arrList.add(“6”);
arrList.add(“7”);
arrList.add(“3″);
System.out.println(” Duplicate Array List :: ” + arrList);

for(int i= 0;i<arrList.size();i++)
{
String value = (String) arrList.get(i);
int startIndex = arrList.indexOf(value);
int endIndex = arrList.lastIndexOf(value);

if(startIndex != endIndex)//If duplicate occur then
{
//if more than one duplicate value in ArrayList
for (int j = startIndex + 1; j < arrList.size(); j++) {
String nextValue = (String) arrList.get(j);
if(value.equalsIgnoreCase(nextValue))
arrList.remove(j);
}
}
}

System.out.println(" Removed duplicate array :: " + arrList);
}

}

Advertisements

One thought on “Find/Removed duplicate value in array ?

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