230k views
1 vote
Given a list of integers, count how many distinct numbers it has. This task can be solved in a single line. ( Python please.)

Example 1:

input: [1, 2, 3, 2, 1]


output: 3

User Dardub
by
8.1k points

1 Answer

2 votes

Answer:

The solution in python to count the number of distinct integers in a list is using the set() function and the len() function.

Example:

input_list = [1, 2, 3, 2, 1]

output = len(set(input_list))

print(output) # 3

Step-by-step explanation:

The set() function will remove any duplicates from the list, and the len() function will return the number of elements in the set, which will be the number of distinct integers in the input list.

User Hakan SONMEZ
by
7.9k points

No related questions found