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
What language is java written?
What is type inference in java8?
What are thread safe functions?
What is the difference between C++ and Java and your preferences?
What do you meant by active and passive objects?
What is a lock or purpose of locks in java?
What is meant by inheritance and what are its advantages?
List out benefits of object oriented programming language?
What is generic class?
What is the meaning of flag day?
What are the advantages of defining packages in java?
a thread is runnable, how does that work? : Java thread
What are different type of exceptions in java?
How do you sort objects in java?
What is dynamic array in java?