帮我写一个for循环判断数组的值是否有相等的代码
时间: 2023-04-03 17:01:47 浏览: 58
好的,以下是代码:
```
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr) / sizeof(int);
for (int i = ; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
printf("数组中有相等的值\n");
return ;
}
}
}
printf("数组中没有相等的值\n");
```
希望能对您有所帮助。
相关问题
帮我写一个for循环判断两个数组的值是否有相等的代码
好的,以下是代码:
```c
int array1[] = {1, 2, 3, 4, 5};
int array2[] = {6, 7, 8, 9, 10};
for (int i = ; i < sizeof(array1) / sizeof(int); i++) {
for (int j = ; j < sizeof(array2) / sizeof(int); j++) {
if (array1[i] == array2[j]) {
printf("数组中有相等的值:%d\n", array1[i]);
break;
}
}
}
```
这段代码会遍历两个数组,判断它们是否有相等的值,如果有,则会输出相等的值。
JS帮我写判断两个数组中其中一个值相等
可以使用双重循环来遍历这两个数组,找到第一个相等的值即可。代码如下:
```javascript
function findEqualValue(arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
return arr1[i];
}
}
}
return null;
}
```
这个函数接受两个参数,分别是需要进行比较的两个数组。函数会返回这两个数组中第一个相等的值,如果没有相等的值则返回null。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)