|
| 1 | +package lk.himash; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public class Illustration_02 { |
| 9 | + public static void main(String[] args) { |
| 10 | + |
| 11 | +// Write a program to find the all repeating numbers & non-repeating numbers & remove duplicates in the following array |
| 12 | + int [] arr = new int [] {5, 4, 2, 5, 3, 8, 5, 2, 1, 8}; |
| 13 | + List<Integer> list = new ArrayList<>(); |
| 14 | + Set<Integer> nonRepeatNo = new HashSet<>(); |
| 15 | + List<Integer> repeatNo = new ArrayList<>(); |
| 16 | + |
| 17 | + for(int i=0; i<arr.length; i++){ |
| 18 | + list.add(arr[i]); |
| 19 | + if(!nonRepeatNo.add(arr[i])) { |
| 20 | + repeatNo.add(arr[i]); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + list.removeAll(repeatNo); |
| 25 | + |
| 26 | + System.out.println("without duplicates : " + list); |
| 27 | + System.out.println("duplicates : " + repeatNo); |
| 28 | + System.out.println("non duplicates : " + nonRepeatNo); |
| 29 | + |
| 30 | + } |
| 31 | +} |
0 commit comments