Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String s = sc.nextLine();
- String[] words = s.split(", ");
- int[] nums = new int[words.length];
- for (int i = 0; i < words.length; i++) {
- nums[i] = Integer.parseInt(words[i]);
- }
- System.out.println(duplicate(nums));
- }
- public static List<Integer> duplicate(int[] nums) {
- List<Integer> duplicatesList = new ArrayList<>();
- List<Integer> nonDuplicates = new ArrayList<>();
- for (int i = 0; i < nums.length; i++) {
- int curNum = nums[i];
- if(!nonDuplicates.contains(curNum)) {
- nonDuplicates.add(curNum);
- }
- else if(duplicatesList.contains(curNum)) {
- break;
- }
- else {
- duplicatesList.add(curNum);
- }
- }
- return duplicatesList.stream().mapToInt(i -> i).boxed().collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement