Calculate the number of distinct values in the list using java

You are given a list of n𝑛 integers, and your task is to calculate the number of distinct values in the list.

Input

The first input line has an integer n𝑛: the number of values.

The second line has n𝑛 integers x1,x2,…,xn𝑥1,𝑥2,…,𝑥𝑛.

Constraints:

  • 1≤n≤2⋅1051≤𝑛≤2⋅105
  • 1≤xi≤1091≤𝑥𝑖≤109

Output

Print one integer: the number of distinct values.

Examples

InputcopyOutputcopy
5 2 3 2 2 32
Note: Copy Paste won’t work here. Invisible text added to the code to prevent copy paste

Solution

import java.util.HashSet;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎
HashSet<Integer> uniqueValues = new HashSet<>();
for (int i = 0; i < n; i++) {
int value = scanner.nextInt();
uniqueValues.add(value);
}

System.out.println(uniqueValues.size());
}
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top