Core Java-ArrayList

Ads
 

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

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
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
Advertisements
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
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
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
core java
core java  how to display characters stored in array in core java
CORE JAVA
CORE JAVA  CORE JAVA PPT NEED WITH SOURCE CODE EXPLANATION CAN U ??   Core Java Tutorials
core java
core java  basic java interview question
core java
core java  surch the word in the given file
CORE JAVA
CORE JAVA  What is called Aggregation and Composition
core java
core java  Hi, can any one exain me the concept of static and dynamic loading in core java
core java
core java  Hi, can any one expain me serialization,Deseralization and exterenalization in core java
core java
core java  how can we justify java technology is robust
Springs Core
Springs Core  what is form backing object in springs core
core java
core java  what is the use of iterator(hase next
core java
core java  please give me following output
Core java
Core java  difference between the string buffer and string builder
Core JAva
Core JAva  how to swap 2 variables without temp in java
core java
core java  how to compare every character in one string with every character in other string
core java
core java  write a java program to view product details from product table
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  its compulsory to save file name and class name is same in java
core java
core java  Hi, Can any one please share a code to print the below: 1 121 12321 1234321
CORE JAVA
CORE JAVA  Tell me some Scenarios why you go for Abstract Class and Interface
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 the max size of array?   You can declare up to maximum of 2147483647
Core Java
Core Java  How to execute cmd command through java?? Give Code of them
Core Java
Core Java  have to find the prime numbers which is less than the current prime numbers using loops
core java
core java  can i use native keyword with abstract method ? if yes explain and if no please explain
CORE JAVA
CORE JAVA  What is Garbage collection in java? What is the role of a developer for garbage collection

Ads