Java) HashSet

IMMINXO·2023년 11월 20일
0

백준 알고리즘 3052번 풀다가 자바에 중복된 값을 허용하지 않는 클래스는 없을까? 라는 생각이 들어 구글링.

HashSet 특징

  • 중복 원소가 들어올 경우 하나만 저장. (중복 원소 허용하지 않음)
import java.util.HashSet;
import java.util.Scanner;

//백준 3052번
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		HashSet<Integer> hash = new HashSet<Integer>();

		for(int i = 0; i<10; i++){
			hash.add(sc.nextInt() % 42);
		}
		sc.close();
		System.out.println(hash.size());
	}
}
profile
까먹지 않기 위한 노력

0개의 댓글

관련 채용 정보