C language to determine whether two arrays have the same elements

Time:2020-2-13

Train of thought:

First create two arrays, a [] and B [] respectively. First compare the first element in array a with all elements in array B, then compare the second element in array a with all elements in array B, and so on. Two for loops are used to generate the subscripts of a array. I loop is used to generate the subscripts of B array. J loop is used to determine whether a [i] is equal to B [J]. If the condition is true, the same element will be generated. Flag is used to mark the state of a program running to a certain moment to judge whether the statement in if is executed.

The system function is used to run the command passed to him in the form of string parameter and wait for the completion of the command. The form is: include < stdlib. H > int system (const char * string);

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a[5] = {5,3,2,65,8};
	int b[8] = {78,8,9,56,3,6,0,7};
	int i = 0;
	int j = 0;
	Int flag = 0; // flag for flag
	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 8; j++)
		{
			if (a[i] == b[j])
			  flag++;	
		}
	}
	if (flag == 0)
	    Printf ("two arrays have no same element \ n");
	else
		Printf ("two arrays have the same element \ n");
	system("pause");
	return 0;
}

In order to realize the code generality, the following optimization is carried out, which is extended to two arrays with any number of elements to achieve this function.

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	int a[] = {34,24,78,5,3};
	int b[] = {23,7,98,5,23,3};
	int i = 0;
	int j = 0;
	For (I = 0; I < sizeof (a) / sizeof (a [0]); I + +) // sizeof (a) / sizeof (a [0]) = 5
	{
		For (J = 0; J < sizeof (b) / sizeof (B [0]); j + +) // sizeof (b) / sizeof (B [0]) = 6
		{
			if (a[i] == b[j])
			{
				Printf ("two arrays have the same element \ n");
				system("pause");
				Return 0; // if the same element exists, it will be returned. Reduce the number of times the loop structure runs
			}
		}
	}
	if (i == sizeof(a) / sizeof(a[0]))
	Printf ("two arrays have no same element \ n");
 
	system("pause");
	return 0;
}

Sizeof() is a capacity measurement function that returns the size of a variable or type in bytes.

Usage: sizeof (type specifier, array name or expression) or sizeof (variable name).

Sizeof (a) is the total size of array a. Sizeof (a [0]) = 4. Since there are five elements in array a in the above code, sizeof (a) = 20, sizeof (a [0]) = 4, so sizeof (a) / sizeof (a [0]) = 5. So no matter how many elements are in the array, no matter what data type, this part of code does not need to be changed.

The following is the supplement of other netizens

We all know that to judge whether there are the same elements in two arrays, only two layers of for loops are needed. But inside the for loop, we need to pay attention to when to jump out of the loop. This is very important. We can’t output all the time, but we can’t output both!
Here is the third approach:

#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int arr1[4] = {0};
	int arr2[5] = {0};
	int i = 0,j=0,k=0;
	Printf ("please enter array 1 (4 elements): \ n");
	for(i = 0;i<sizeof(arr1)/sizeof(arr1[0]);i++)
	{
		scanf("%d",&arr1[i]);
	}
	Printf ("please enter array 2 (5 elements): \ n");
	for(i=0;i<sizeof(arr2)/sizeof(arr2[0]);i++)
	{
		scanf("%d",&arr2[i]);
	}
 
	for(j = 0; j<sizeof(arr1)/sizeof(arr1[0]); j++)
	{
		for(k = 0;k<sizeof(arr2)/sizeof(arr2[0]);k++)
		{
			if(arr1[j] == arr2[k])
			{
				Printf ("has the same element! \n "";
			}
			if(k<sizeof(arr2)/sizeof(arr2[0]))	
			{
				break;
			}
		}
		if(j>=sizeof(arr1)/sizeof(arr1[0]))	
		Printf ("no identical elements! \n "";
	}
	return 0;
}

The above method needs to judge more, and the judgment place should also be found. The following method will be simpler:

#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int arr1[4] = {0};
	int arr2[5] = {0};
	int i = 0,j=0,k=0;
	Printf ("please enter array 1 (4 elements): \ n");
	for(i = 0;i<sizeof(arr1)/sizeof(arr1[0]);i++)
	{
		scanf("%d",&arr1[i]);
	}
	Printf ("please enter array 2 (5 elements): \ n");
	for(i=0;i<sizeof(arr2)/sizeof(arr2[0]);i++)
	{
		scanf("%d",&arr2[i]);
	}
 
	for(j = 0; j<sizeof(arr1)/sizeof(arr1[0]); j++)
	{
		for(k = 0;k<sizeof(arr2)/sizeof(arr2[0]);k++)
		{
			if(arr1[j] == arr2[k])
			{
				Printf ("has the same element! \n "";
				return 0;
			}
		}
	}
	Printf ("no identical elements! \n "";
	return 0;
}

OK, this article introduces this first. I hope you can support developepaer more later.

Recommended Today

[Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat: 127.0.0.1:6379> keys * 1) “kkk” 2) “key1” 127.0.0.1:6379> randomkey “key1” 127.0.0.1:6379> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]