2016 Java Chapter 19 (Week 15)

From: 2016-11-23 00:00:00 To: 2016-12-28 00:00:00 Now: 2024-11-21 22:31:47 Status: Public

A - 중복된 값을 가지지 않는 ArrayList

Time Limit: 1s Memory Limit: 128MB

Snippet Judge Submissions: 39 Solved: 27
Description

ArrayList를 입력받아 중복된 값을 가지지 않는 새로운 ArrayList를 return 하는 Method를 작성하시오. 

(Distinct elements in ArrayList) Write the following method that returns a new ArrayList. The new list contains the non-duplicate elements from the original list.

 

Input

* Line 1 : 자료형 (String, Integer, Double 중 하나)

* Line 2 : 자료의 개수 N (1~1,000 범위의 정수) 

* Line 3 ~ N+2 : 배열의 원소

 

Output

* Line 1 ~ ? : 중복이 제거된 배열의 원소

 

Sample Code
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String type = sc.next();
        int N = sc.nextInt();

        if (type.compareTo("String") == 0) {
            ArrayList<String> list1 = new ArrayList<String>();
            for (int i = 0; i < N; i++) list1.add(sc.next());
            for (String val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        } else if (type.compareTo("Integer") == 0) {
            ArrayList<Integer> list1 = new ArrayList<Integer>();
            for (int i = 0; i < N; i++) list1.add(sc.nextInt());
            for (int val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        } else if (type.compareTo("Double") == 0) {
            ArrayList<Double > list1 = new ArrayList<Double >();
            for (int i = 0; i < N; i++) list1.add(sc.nextDouble());
            for (double val : removeDuplicates(list1)) {
                System.out.println(val);
            }
        }
    }

    YOUR_CODE
}
Sample Input
Double
10
2.9
2.9
2.3
1.1
2.3
1.0
1.1
2.3
2.6
2.7
Sample Output
2.9
2.3
1.1
1.0
2.6
2.7
Hint
ArrayList<E> removeDuplicates(ArrayList<E> list)
Source

JAVA2015 PE19.3