Core Java-ArrayList



Share on Google+Share on Google+

Pintu Ranjan Sai
Core Java-ArrayList
1 Answer(s)      6 years and a month ago
Posted in : Java Interview Questions

How do i find duplicates in ArrayList. First i add some elements to ArrayList, then how do i find the duplicates and show the duplicate elements. Give an example

View Answers

May 4, 2011 at 12:29 PM

import java.util.*;
class FindDuplicates
    public static <T> List getDuplicate(Collection<T> list) {

    final List<T> l = new ArrayList<T>();
    Set<T> set = new HashSet<T>() {
    public boolean add(T e) {
        if (contains(e)) {
        return super.add(e);
   for (T t : list) {
    return l;

public static <T> boolean hasDuplicate(Collection<T> list) {
    if (getDuplicate(list).isEmpty())
        return false;
    return true;

    public static void main(String[] args) 
        Scanner input=new Scanner(;
        System.out.print("How many elements do you want to add in a list: ");
        int no=input.nextInt();
        ArrayList<String> list=new ArrayList<String>();
        for(int i=0;i<no;i++){
        System.out.println("Duplicate Elemnts are: ");
            List l=getDuplicate(list);
            for(int i=0;i<l.size();i++){
            System.out.println("There is no duplicate elements in the list.");



Related Tutorials/Questions & Answers:
Core Java-ArrayList
Core Java-ArrayList  How do i find duplicates in ArrayList. First i add some elements to ArrayList, then how do i find the duplicates and show the duplicate elements. Give an example
Java ArrayList
Java ArrayList   How to declare unique ArrayList
java arraylist
help how to handle this through java arraylist. xml : <?xml version=' arraylist  Hi, Thanks in advance.. From the xml given below i need to create arraylist whose name is same as type value(e.g : list1,list2
Java arraylist, linkedlist
Java arraylist, linkedlist  What is the major difference between LinkedList and ArrayList
Java ArrayList Example
Java ArrayList Example  How can we use array list in java program..."); array.add("Arun"); System.out.println("ArrayList Size...++){ System.out.println("ArrayList Element"+i+" :"+array.get(i
Java arraylist duplicate elements search
Java arraylist duplicate elements search  hi, please help me finding the no of duplicates elements in an arraylist example an arraylist contains elements like: mac raj mohan frank mac tom raj mohan frank result: mac 2 raj
Java arraylist iterator
ArrayList has iterator() method.Using iterator() method  elements of the arraylist easily gets retrieved. iterator() can be used in the for loop. It is clear by the given example. Example of Java Arraylist
Java arraylist of arraylist
Java arrayList can make the use of other arrayList.  In one arrayList other arrayList can be added as the object  It works as two dimension array. Example of Java Arraylist of Arraylist import
Java ArrayList sublist
It returns the part of the ArrayList as List reference. part is the given range between two indexes. Java Arraylist Sublist Example import...) { String  ar[]={"india","pakistan","United Kingdom","Japan","Korea"}; ArrayList
Java ArrayList removeall
the elements from list1 which is present in the list2 Java Arraylist Removeall...","Korea"}; ArrayList list=new ArrayList(); ArrayList list1=new ArrayList
Java arraylist merge
is joined in to the list1. Example Java Arraylist Merge import... In Java Two arrays can be joined by using the Collection list...};         List list1=new ArrayList();         List list2=new ArrayList
Java arraylist to array
Java arrayList has method toArray() It converts the given list... be used properly. Example of Java Arraylist to Array import...) {      int ar[]={444,222,333,111};      ArrayList list=new ArrayList
Java ArrayList clear
It removes all the elements of the given list and returns empty list But the remove method removes element one by one Java Arraylist Clear... list=new ArrayList(); list.add(ar[0]); list.add(ar[1]); list.add(ar[2
Java ArrayList Addall
list. Java Arraylist Addall Example import java.util.*; public...","pakistan","United Kingdom","Japan"}; List list=new ArrayList(); list.add(ar... ArrayList(); list1.add("delhi"); list1.add("islamabad"); list1.add("britain
Java ArrayList clone
It returns the copy of the given ArrayList object as the Object class. So it must be casted to the ArrayList for further use. Java Arraylist...","Japan"}; ArrayList list=new ArrayList(); list.add(ar[0]); list.add(ar[1
Java arraylist of objects
In arrayList object is added by default This can be object of String, Integer or any Wrapper class Example of Java Arraylist... ArrayList(4);        list.add(0,name2);        list.add(1,name1
Array list in Java - arraylist type error
Array list in Java - arraylist type error  I am using Array list in Java but when i compile i get array list type error. Can anyone please explain what is array list type error and why it occurs
Java arraylist sort
of Java Arraylist Sort import java.util.ArrayList; import... ArrayList doesn't have sort() method. We can use the static sort ... main(String[] args) {      Integer ar[]={444,222,333,111};      ArrayList list
Java arraylist remove
remove() method is used to remove the element of the arrayList. It has two forms remove(Object) remove(index) Java Arraylist Remove Example...(String[] args) { Integer ar[]={111,222,333,444}; List list=new ArrayList
Retrieve all the students in the same year(Java ArrayList)?
Retrieve all the students in the same year(Java ArrayList)?  FoundStudents.jsp year- parameter I receive from a form search- is an object of type... = search the ArrayList students starting from this index, not from the beginning
Jsp Core
Jsp Core   
core java
core java  how to display characters stored in array in core java
Core Java
Core Java  what is a class
core java
core java  i need core java material   Hello Friend, Please visit the following link: Core Java Thanks
CORE JAVA  What is called Aggregation and Composition
core java
core java  surch the word in the given file
core java
core java  basic java interview question
core java
core java  Hi, can any one expain me serialization,Deseralization and exterenalization in core java
core java
core java  Hi, can any one exain me the concept of static and dynamic loading in core java
Core JAva
Core JAva  how to swap 2 variables without temp in java