Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Core Java-ArrayList

Ads

 
 

Share on Google+Share on Google+

Pintu Ranjan Sai
Core Java-ArrayList
1 Answer(s)      6 years and 3 months 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

Ads
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)) {
            l.add(e);
        }
        return super.add(e);
    }
    };
   for (T t : list) {
        set.add(t);
    }
    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.in);
        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++){
            String st=input.next();
            list.add(st);
        }
        System.out.println("Duplicate Elemnts are: ");
        if(hasDuplicate(list)){
            List l=getDuplicate(list);
            for(int i=0;i<l.size();i++){
                System.out.println(l.get(i).toString());
            }
        }
        else{
            System.out.println("There is no duplicate elements in the list.");
        }

    }
}

Ads









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
Advertisements
java arraylist
help how to handle this through java arraylist. xml : <?xml version='1.0...java 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 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 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 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 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
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
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
Java arraylist to string array
arrayList can be converted into string array. For this first converts the arrayList into the object array. This is done by  using the toArray...]; ArrayList list=new ArrayList(); list.add(ar[0]); list.add(ar[1]); list.add
Java arraylist index() Function
Java arrayList has index for each added element. This index starts from 0. arrayList values can be retrieved by the get(index) method. Example of Java Arraylist Index() Function import
Java ArrayList removerange
removeRange() is a protected method of the Java ArrayList. It removes... be used. Java Arraylist Remove range Example import java.util.*; public class List1 extends ArrayList{ public static void main(String[] args
Java arraylist foreach
Example Java arraylist foreach import java.util.ArrayList; import... In the arraylist, foreach loop is easily used. It takes element...) {         List list=new ArrayList();         String names[]={"mac","john
Iterate java Arraylist
of the ArrayList can be traversed by the Iterator. Iterator has methods hasNext() and next(). Example of Java Arraylist Iterate import java.util.*; public class... Iterator is an interface in the collection framework. ArrayList
Java arraylist contains
or not. It returns boolean value i.e. true or false. Example of Java Arraylist Contains import java.util.ArrayList; import java.util.List; public...) {         List list=new ArrayList();         String names[]={"mac","john","alexender","rock
Java arraylist for loop
Elements of the ArrayList can be easily added and retrieved by the for loop. Example FOR Java arraylist for loop import java.util.ArrayList...(String[] args) {         List list=new ArrayList();         String names
Java ArrayList indexof() Method
. If no element is present then returns the -1. Example of Java Arraylist... It returns the index of the given element present in the ArrayList...","Japan"}; List list=new ArrayList(); list.add(ar[0]); list.add(ar[1
Java ArrayList listIterator
. It works similar to the iterator() method. Java Arraylist List Iterator..."}; List list=new ArrayList(); list.add(ar[0]); list.add(ar[1]); list.add(ar[2
Java arraylist int
. Example of Java Arraylist int import java.util.ArrayList; import java.util.List... arraylist can contain int data type elements also.  Only...) {         List list=new ArrayList();        int arr1[]={11,12,13,14,15
Jsp Core
Jsp Core