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 is the difference between variable & constant?

536


What is hashing principle in java?

546


What is arrays sort in java?

566


Hi all, I am dng a mini project on FileSplitter application which splits the GBs of logfile into Smaller chunks(mbs) depending on the split size." How to handle GBs file? I am getting OutOfMemoryException, when I input such GB sized file. Thx

1596


What about features of local inner class?

611






What is a java developer salary?

534


Can we define package statement after import statement in java?

552


What are the differences between getting and load method?

534


What is the use of isempty in java?

531


What is the difference between break and continue statements?

543


Can you have two constructors in java?

491


What is hash code collision?

580


How garbage collection is done in java?

542


What is advantage of java?

534


Why synchronization is important?

582