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
Can a class be private or protected in java?
What are controls and their different types in awt?
What is the significance of continue jump statement? Explain with an example.
How do you change an int to a string?
What is final, finally, finalize?
Why is stringbuffer faster than string?
What are the important features of Java 10 release?
Can we use catch statement for checked exceptions?
What is the difference between yielding and sleeping?
Write a java program for binary search?
Does java have a compiler?
What is an exception in java?
What is java instanceof operator?
we have syntax like for(int var : arrayName) this syntax is to find whether a number is in the array or not.but i want to know how to find that number's location.
What is meant by tab pans?