How do I know whether a different array of all elements of the current ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I know whether a different array of all elements of the current ?

Example: int[] nums_1 = {1, 2, 3, 4}; int[] nums_2 = {1, 2, 3, 4, 5, 6}; Must be true It is necessary to find out whether all the values of one are there in another Example_2: int [] nums_1 = { 1, 7 , 3, 4} ; int [] nums_2 = { 1 , 2 , 3, 4 , 5, 6} ; Conclusion: a lie because there is no element 7 in the other array.

27th Jul 2016, 2:08 PM
Shwarz Andrei
2 Answers
+ 4
Solved by short method: public boolean isContain(int[] n1, int[] n2){ int count = 0; for (int a : n1) for (int b : n2) if (a == b) { count++; break;} return count == n1.length;} Example - 1: int[] n1 = {1, 2, 3, 4}; int[] n2 = {1, 2, 3, 4, 5, 6}; System.out.println(isContain(n1,n2)); Output: true Example - 2: int[] n1 = {1, 8, 3, 4}; int[] n2 = {1, 2, 3, 4, 5, 2}; System.out.println(isContain(n1,n2)); Output: false
28th Jul 2016, 1:21 PM
Shwarz Andrei
0
You can write a code even with better complexity than the above The second array must be sorted....if not Sort it. Then: for each x in arr_1: if(binarysearch(arr_2,x)==true): count++ if(count==arr_1.length()) print("Yes") else print("No")
9th Aug 2016, 5:13 AM
Priyanshu Kumar
Priyanshu Kumar - avatar