This Chat is read-only. Login to resume chatting.
#include <stdbool.h>
#include <stdlib.h>
typedef struct {
unsigned int len;
int* val;
} Tab;
bool has_duplicates(Tab tab) {
if (tab.len == 0) {
return false; // Un tableau vide n'a pas de duplications
}
// Allouer un tableau auxiliaire pour suivre les éléments vus
bool* seen = (bool*)calloc(tab.len, sizeof(bool));
if (seen == NULL) {
// Gestion de l'erreur d'allocation mémoire
return false;
}
for (unsigned int i = 0; i < tab.len; i++) {
int value = tab.val[i];
// Vérifier si la valeur a déjà été vue
for (unsigned int j = 0; j < i; j++) {
if (tab.val[j] == value) {
free(seen);
return true; // Duplication trouvée
}
}
seen[i] = true;
}
free(seen);
return false; // Aucune duplication trouvée
}
int main() {
// Exemple d'utilisation
Tab tab = {5, (int[]){1, 2, 3, 4, 5}};
if (has_duplicates(tab)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
Tab tab2 = {5, (int[]){1, 2, 3, 4, 4}};
if (has_duplicates(tab2)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
return 0;
}#include <stdbool.h>
#include <stdio.h>
typedef struct {
unsigned int len;
int* val;
} Tab;
bool has_duplicates(Tab tab) {
for (unsigned int i = 0; i < tab.len; i++) {
for (unsigned int j = i + 1; j < tab.len; j++) {
if (tab.val[i] == tab.val[j]) {
return true; // Duplication trouvée
}
}
}
return false; // Aucune duplication trouvée
}
int main() {
// Exemple d'utilisation
Tab tab = {5, (int[]){1, 2, 3, 4, 5}};
if (has_duplicates(tab)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
Tab tab2 = {5, (int[]){1, 2, 3, 4, 4}};
if (has_duplicates(tab2)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
return 0;
}#include <stdbool.h>
#include <stdio.h>
typedef struct {
unsigned int len;
int* val;
} Tab;
bool has_duplicates(Tab tab) {
for (unsigned int i = 0; i < tab.len; i++) {
for (unsigned int j = i + 1; j < tab.len; j++) {
if (tab.val[i] == tab.val[j]) {
return true; // Duplication trouvée
}
}
}
return false; // Aucune duplication trouvée
}
int main() {
// Exemple d'utilisation
Tab tab = {5, (int[]){1, 2, 3, 4, 5}};
if (has_duplicates(tab)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
Tab tab2 = {5, (int[]){1, 2, 3, 4, 4}};
if (has_duplicates(tab2)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
return 0;
}#include <stdbool.h>
#include <stdio.h>
typedef struct {
unsigned int len;
int* val;
} Tab;
bool has_duplicates(Tab tab) {
for (unsigned int i = 0; i < tab.len; i++) {
for (unsigned int j = i + 1; j < tab.len; j++) {
if (tab.val[i] == tab.val[j]) {
return true; // Duplication trouvée
}
}
}
return false; // Aucune duplication trouvée
}
int main() {
// Exemple d'utilisation
Tab tab = {5, (int[]){1, 2, 3, 4, 5}};
if (has_duplicates(tab)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
Tab tab2 = {5, (int[]){1, 2, 3, 4, 4}};
if (has_duplicates(tab2)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
return 0;
}#include <stdbool.h>
#include <stdio.h>
typedef struct {
unsigned int len;
int* val;
} Tab;
bool has_duplicates(Tab tab) {
for (unsigned int i = 0; i < tab.len; i++) {
for (unsigned int j = i + 1; j < tab.len; j++) {
if (tab.val[i] == tab.val[j]) {
return true; // Duplication trouvée
}
}
}
return false; // Aucune duplication trouvée
}
int main() {
// Exemple d'utilisation
Tab tab = {5, (int[]){1, 2, 3, 4, 5}};
if (has_duplicates(tab)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
Tab tab2 = {5, (int[]){1, 2, 3, 4, 4}};
if (has_duplicates(tab2)) {
printf("Le tableau contient des duplications.\n");
} else {
printf("Le tableau ne contient pas de duplications.\n");
}
return 0;
}