I am looking to return true if all the elements in A are found within B and false otherwise. Currently, I have written the code below but the function seems to only return false.
public static boolean con(int[] a, int[] b) {
if (a == null || a.length == 0) {
return false;
}else if (b == null || b.length == 0) {
return false;
}
for (int i = 0; i < a.length; i )
{
for (int j = i 1; j < b.length;j )
{
if (a[i] == (b[j])) {
return true;
}
}
}
return false;
}
CodePudding user response:
It is easy to make mistakes when you program with indices. Luckily, in this case there is no need to do that:
public static boolean con(int[] a, int[] b) {
loop: for (int ai : a) {
for (int bi : b) {
if (ai == bi) {
continue loop;
}
}
return false;
}
return true;
}
Always use the strengths of the language, not the weaknesses.
CodePudding user response:
There are a couple of issues in your code as pointed out in the comments that are causing your code to return false, however, the other issue is that your return true if a[i] == (b[j])
which will be true and cause a return if the first element in the array is a match regardless of the rest of the array and if it matches or not.
Here is a working version with comments that help explain what has been changed:
public static boolean con(int[] a, int[] b) {
//Use a flag to track if the integers are found
Boolean flag = true;
//return false for null or 0 length
if (a == null || a.length == 0 || b == null || b.length == 0) {
return false;
}
//Check integers
for (int i = 0; i < a.length; i ){
//Use an inner flag to track each integer
Boolean innerFlag = false;
//Start the inner loop from 0 and incriment it
//Only use j = i if you need the integers to be in order?
for (int j = 0; j < b.length; j ){
if (a[i] == (b[j])) {
//we can not return from the method until all elements are checked
//so set the flag instead and break so the next element can be checked
innerFlag = true;
break;
}
}
//if an element does not exist we can set the flag false and break and return immediatly
if (innerFlag == false){
flag = false;
break;
}
}
return flag;
}
CodePudding user response:
Why you use the int j = i 1 and not just j to compare all b from the beginning of the array with j = 0... anyway if you change that and test it with two equals filled array it will give true at the first element, and if the array are different will give you true when both coincide, but you are not storing the results in any place, I think what you could do is add a counter with the length of a:
for (int i = 0; i < a.length; i ) {
for (int j = 0; j < b.length; j ) {
if (a[i] == (b[j])) {
count--;
if(count == 0)
return true;
}
It kinda work, test it thoroughly and tell me.
CodePudding user response:
If you really want to do it manually, you could use something like this:
private static boolean containsAll(Integer[] arrayA, Integer[] arrayB) {
Set<Integer> matches = new HashSet<>();
boolean containsAll = false;
for (int i = 0; i < arrayA.length; i ) {
for (int j = 0; j < arrayB.length; j ) {
if (arrayA[i] == arrayB[j]) {
matches.add(arrayA[i]);
}
}
}
if (matches.size() == arrayA.length) {
containsAll = true;
}
return containsAll;
}
Since a set
can only store unique values, even if arrayB
has duplicated elements, it will still work. It won't work if arrayA
has duplicated elements, though.