Python array programme Codechef solution
Problem
Given an array A of size N, check if there exists any pair of index i and j such that Ai=Aj and i ≠ j.
Input
-
The first line of the input contains a single integer, T, denoting the number of test cases.
-
The first line of each test case contains the integer N.
-
The second line of each test case contains N space-separated integers Ai.
Output
For each test case, print a single line containing the answer "Yes" or "No" (without quotes).
Constraints:
-
1<=T<=100
-
2<=N<=10^5
-
1<=Ai<=10^9
-
The sum of N over all test cases doesn’t exceed 106.
Sample Input:
2
4
1 2 1 3
5
5 4 1 2 3
Sample Output:
Yes
No
Solution:
try:
T = int(input(“Enter the number of terms: “))
for i in range(T):
N = int(input(“Enter a number: “))
A = list(map(int, input(“Enter numbers: “).split()))
unique_values = set(A)
if len(unique_values) < N:
print("Yes")
else:
print("No")
except EOFError:
pass
Steps to solve this problem:
-
In the try block, ask the user to enter a number of terms.
-
In the loop, ask the user to enter a number and store it in N.
-
Next, Ask users to enter numbers separated by space, and using the map() function, get iterator objects, and using the list() function, convert those objects to a list and store it in A
-
Convert list A to a set using the set() function and store it in unique_values.
-
Now check the length of unique_values; if it is less than N, then print yes; otherwise, no.
-
In the except block, we just left it empty, so we used the pass statement in it.