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
Is there a jre for java 11?
Is vector synchronized in java?
What is the main purpose of java?
Who developed java?
What are the advantages of encapsulation in java?
What is the difference in between cpp and java? Can u explain in detail?
explain the concept of virtual method invocation in polymorphism in detail?
Why for each loop is used?
why Java does not support multiple inheritances?
What will be the default values of all the elements of an array defined as an instance variable?
What is the use of arraylist in java?
What is a for loop in java?
what is object slice?
What is the difference between delete and delete[]
How do I convert a numeric ip address like 192.18.97.39 into a hostname like java.sun.com?