#include <stdio.h>
int is_two_same(int size, int a[]) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (a[i] == a[j]) {
return 1;
}
}
}
return 0;
}
int main() {
int size;
scanf("%d", &size);
int a[size];
for (int i = 0; i < size; i++) {
scanf("%d", &a[i]);
}
if (is_two_same(size, a)) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
This code checks if any two elements in an array are equal. It reads an array size and values, then uses nested loops to compare each pair of elements. If a duplicate is found, it prints "YES", otherwise "NO".
- The function `is_two_same` uses two loops to compare elements, but the outer loop condition `i < size - 1` is correct because the last element is compared in the inner loop. However, think about what happens when `size` is 0 or 1. The loops may not run, which is fine, but consider if the function should handle those edge cases differently.
- In `main`, you declare `int a[size];` after reading `size`. This is a variable-length array, which is allowed in C99 but not in all C standards. If your compiler supports it, it works, but be aware that some environments may not accept this.