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 / srivatsava
import java.util.*;
public class ArrayListDuplicateValueDeleteExample {
/**
* ArraList Duplicate values removed without using Set.
*/
public static void main(String[] args) {
ArrayListDuplicateValueDeleteExample obj = new ArrayListDuplicateValueDeleteExample();
ArrayList al = new ArrayList();
ArrayList al1 = new ArrayList();
al.add("A");
al.add("B");
al.add("B");
al.add("B");
al.add("B");
al.add("C");
al.add("A");
al.add("A");
al.add("A");
al.add("A");
al.add("A");
System.out.println("Size of the array - "+al.size());
System.out.println("ArrayList Values with Duplicate - "+al);
for(int i=0;i<al.size();i++){
if(al.contains(al.get(i))){
if (al1.contains(al.get(i))){
}else {
al1.add(al.get(i));
}
}
}
System.out.println("New ArrayList Values without Duplicate - "+al1);
}
}
| Is This Answer Correct ? | 8 Yes | 0 No |
Post New Answer View All Answers
What are different types of multitasking?
If try block is successfully executed, Then Is Finally block executed?
What is the purpose of an interface?
What is the purpose of extern variable?
What's the base class in java from which all classes are derived?
What is a parent class in java?
Is java pass by value or pass by reference?
Can you declare the main method as final?
What are encapsulation, inheritance and polymorphism?
What is a final class in java?
Differentiate between stringbuffer and stringbuilder in java.
Why do I need to declare the type of a variable in java?
Will set allow duplicates in java?
Is singleton class thread safe?
What is the difference between choice and list?