Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int t;
- cin >> t;
- while (t--) {
- long long n,x;
- long long dem=0; // Khởi tạo biến dem để đếm số lượng phần tử x
- cin >> n >> x;
- long long arr[n];
- for(int i = 0; i< n; i++){
- cin >> arr[i];
- }
- for(int i = 0; i<n; i++){ // Kiểm tra từng phần tử trong mảng
- if(arr[i]==x){ // Nếu có phần tử nào trong mảng bằng x
- dem++; // Thì tăng biến dem lên 1
- }
- }
- if(dem==0){ // Nếu dem = 0 (Trong mảng Không có phần tử x)
- cout << -1 << endl; // Thì in ra -1
- }else{
- cout << dem << endl; // Còn lại thì in ra biến dem (Số lượng phần tử x trong mảng)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement