알고리즘
오름차순으로 정렬된 정수형 배열에서 중복을 제외한 종류의 수를 계산하는 함수
개발 초보
2023. 4. 27. 11:51
※조건: 배열은 오름차순으로 정렬되어 입력된다.
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static final Scanner scanner = new Scanner(System.in);
/**
* 중복을 제외한 숫자의 종류의 수를 계산하는 함수
* @param data 원본 배열
* @param n 원본 배열의 크기
* @return 숫자의 종류의 수
*/
public static int getElementTypeCount(int[] data, int n){
int countType = 0;
for(int i = 0; i < n; i++){
//data[i] 모든 원소가 차례로 한 번씩 순서대로 등장한다
if(i == 0 || data[i-1] != data[i]){
countType += 1;
}
}
return countType;
}
public static void main(String[] args) throws Exception {
int n = scanner.nextInt();
int[] data = new int[n];
for(int i = 0 ; i < n ; i++){
data[i] = scanner.nextInt();
}
int answer= getElementTypeCount(data, n);
System.out.println(answer);
}
}