I have a Arraylist object, it has duplecate values also. Now
question is i want delete duplecate data in that objet with
out using Set?

Answer Posted / sreenivas

Hi,
I hope the answer I am providing is the easiest one among
all others

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

public class ArrayListDemo
{
public static void main(String[] args)
{
ArrayList containsDuplicate = new ArrayList
();

containsDuplicate.add("A");
containsDuplicate.add("B");
containsDuplicate.add("C");
containsDuplicate.add("D");
containsDuplicate.add("A");
containsDuplicate.add("B");
containsDuplicate.add("C");
containsDuplicate.add("A");

ArrayList noDuplicate = new ArrayList
(containsDuplicate);
ArrayList fresh = new ArrayList();

System.out.println("Hello World!!!!!!!!
with duplicates " + containsDuplicate.size());
System.out.println("Hello World!!!!!!!!
with duplicates " + noDuplicate.size());
for(int i =0;i<containsDuplicate.size();i++)
{
String outerObject = (String)
containsDuplicate.get(i);
int count = 0;
for(int j=0;j<noDuplicate.size
();j++)
{
String innerObject =
(String) noDuplicate.get(j);
if(outerObject.equals
(innerObject))
{
if((count == 0))
{
count++;
// fresh.add
(outerObject);
}
else
{

noDuplicate.remove(j);

}
}

}
}

for(int i=0; i< noDuplicate.size();i++)
{
System.out.println("Each
Element :::::::: " + noDuplicate.get(i));
}
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are basic keywords?

559


What is the purpose of using java.lang.class class?

530


Explain the concept of proper inheritance?

623


Does apple use java?

498


Should database connections be singleton?

570






How to avoid memory leak in java?

601


What is a method in programming?

641


What is the main method java?

535


What is the use of put method?

520


What is a methodologist?

534


Can you give few examples of final classes defined in java api?

567


What are the different access modifiers available in java?

563


Define how objects are stored in java?

567


When do we use hashset over treeset?

538


What is ctrl m character?

518