You are here : Home / Core Java Tutorials / Interview Programs (beginner to advanced) in java / Level1 programs for (beginner)
In this core java programming tutorial we will write a program to Find all duplicate numbers in list in java.
Q. Find all duplicate numbers in list in java.
Example:
list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10]
Duplicate numbers in list are: 5 10.
Full Program/SourceCode / Example to Find all duplicate numbers in list in java>
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
import java.util.ArrayList;
import java.util.List;
public class FindDuplicatNumberInListExample {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 10; i++) {
list.add(i);
}
list.add(5); //add duplicate number in list.
list.add(10); //add another duplicate number in list.
findDuplicateNumbersInList(list);
}
/*
* Method prints duplicate numbers in List and returns list of non-duplicate numbers.
*/
public static List<Integer> findDuplicateNumbersInList(List<Integer> list) {
List<Integer> listWithoutDuplicates = new ArrayList<Integer>();
System.out.println("list is: "+list);
System.out.print("Duplicate numbers in list are: ");
for (int i : list) {
if (listWithoutDuplicates.contains(i))
System.out.print(i+" ");
else
listWithoutDuplicates.add(i);
}
return listWithoutDuplicates;
}
}
/*OUTPUT
list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10]
Duplicate numbers in list are: 5 10
*/
|
So in this core java programming tutorial we wrote a program how to Find all duplicate numbers in list in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
>42) How to synchronize arraylist in java to make it completely thread safe in java
>43)sorting list by implementing Comparable and Comparator in java
>52) how to interrupt or STOP thread in java
>53) write Threads addShutdownHook in java
>54) Handling uncaught runtime exception generated in run method
>55) Demonstrate ThreadGroup's important methods in DETAIL
>56) Implementing Threads in java by implementing Runnable interface and extending Thread class
>57) How Thread behaviour is unpredictable?
>58) ensure all threads that started from main must end in order in which they started and also main should end in last?
>60) Race condition in multithreading and it's solution
>48) Program to sort Employee list on basis of name in descending order
>49 Program to sort Integer array using Arrays.sort
>50) find whether current thread holds the lock on monitor of specified object
>51) set Thread priorities>49 Program to sort Integer array using Arrays.sort
>50) find whether current thread holds the lock on monitor of specified object
>52) how to interrupt or STOP thread in java
>53) write Threads addShutdownHook in java
>54) Handling uncaught runtime exception generated in run method
>55) Demonstrate ThreadGroup's important methods in DETAIL
>56) Implementing Threads in java by implementing Runnable interface and extending Thread class
>57) How Thread behaviour is unpredictable?
>59) Volatile example in java?
Labels:
Core Java
Level1 programs (beginners)