Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


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


Advertisement
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.");
        }

    }
}


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 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  CORE JAVA PPT NEED WITH SOURCE CODE EXPLANATION CAN U ??   Core Java Tutorials
core java
core java  i need core java material   Hello Friend, Please visit the following link: Core Java Thanks
CORE JAVA
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
Core java
Core java  difference between the string buffer and string builder
core java
core java  please give me following output
core java
core java  what is the use of iterator(hase next
Springs Core
Springs Core  what is form backing object in springs core
core java
core java  how can we justify java technology is robust
Core java
Core java  how to convert reverse of String without using String function
core java
core java  Is it possible to create a shallow copy of an arraylist instance? if yes then please post the code
CORE JAVA
CORE JAVA  What is Garbage collection in java? What is the role of a developer for garbage collection
CORE JAVA
CORE JAVA  Tell me some Scenarios why you go for Abstract Class and Interface
core java
core java  Hi, Can any one please share a code to print the below: 1 121 12321 1234321
core java
core java  can i use native keyword with abstract method ? if yes explain and if no please explain
core java
core java  its compulsory to save file name and class name is same in java
Core Java
Core Java  have to find the prime numbers which is less than the current prime numbers using loops
Core Java
Core Java  How to execute cmd command through java?? Give Code of them
Core Java
Core Java  Write a Program to add given number of days to the current system date and display the same
core java
core java  what is the max size of array?   You can declare up to maximum of 2147483647
core java
core java  write a java program to view product details from product table
core java
core java  how to compare every character in one string with every character in other string
Core Java
Core Java  What is the significance of static synchronized method? Why do we have the method declared as static synchronized
Advertisements
 

 

 

DMCA.com