0

I've been trying at this for the entire day. I have an assignment and basically I need to check if 2 arrays have the same elements (w/o having the same order. My idea is

1- check if theyre the same length. if not then theyre obviously not the same

2- to read one element in array 1 and then look for it in the other array, then increment a counter and at the end check if the counter is equal to the length of the array.

i'm getting this error: missing return statement

i need to say that i'm not allowed to use things like sort or hash to solve this since i've seen methods with these before but they cant help here.

public static boolean isSame(int[] ArrayX,int [] ArrayY){

       int count=0;

   if(ArrayX.length!=ArrayY.length)// to check that they have the same size
       return false;

 for (int i=0;i<ArrayX.length;i++){

       for(int j=0;j<ArrayY.length;j++)//this loop is to search for element[i] in array Y
          {
           if (ArrayY[j]==ArrayX[i])

               count++;
           }
           }
 if (count==ArrayX.length) //to check that number of identical elements is equal to the size 
  return true;
   }

 

7 Answers7

0

You're just missing a default return statement in the end. Add "return false" when they're not equal

if (count==ArrayX.length) {
  return true;
}
return false;

Also curious, You mentioned you cannot use hash. Are you referring to hash based data structures?

0

You need to return default condition when your condition is false. Updated the code.

public static boolean isSame(int[] ArrayX,int [] ArrayY){

       int count=0;

   if(ArrayX.length!=ArrayY.length)// to check that they have the same size
       return false;

 for (int i=0;i<ArrayX.length;i++){

       for(int j=0;j<ArrayY.length;j++)//this loop is to search for element[i] in array Y
          {
           if (ArrayY[j]==ArrayX[i])

               count++;
           }
           }
 if (count==ArrayX.length) //to check that number of identical elements is equal to the size 
  return true;
   }
return false;
}
Rahul Singh
  • 690
  • 1
  • 5
  • 10
0

You can simply order the two arrays and then loop through both. If either their count is mismatched or the ordering isn't the same, you can efficiently return the failure.

Avoids nesting for loops!

import java.util.Arrays;

public static boolean isSame(int[] ArrayX, int [] ArrayY)
{    
    if (ArrayX.length != ArrayY.length) {// to check that they have the same size
        return false;
    }

    ArrayX = Arrays.sort(ArrayX);
    ArrayY = Arrays.sort(ArrayY);
    
    for (int i=0; i < ArrayX.length; i++){
        
        if (ArrayX[i] != ArrayY[i]){
            return false;
        }
    }

    return true;
}

Note: this hasn't been tested. Please comment if it needs amending. Thank-you.

Matthew Spence
  • 986
  • 2
  • 9
  • 27
0

You can just return the condition and avoid extra if else.

public static boolean isSame(int[] ArrayX,int [] ArrayY){

        if(ArrayX.length!=ArrayY.length)// to check that they have the same size
            return false;

        // Sort both arrays
        Arrays.sort(ArrayX);
        Arrays.sort(ArrayY);
        for (int i=0;i<ArrayX.length;i++){
            if (ArrayX[i]!=ArrayY[i]){ // Compare elements
                return false;  // 
            }
        }
        return true;
    }

Time complexity is ( O(n log(n)) + O(n log(n)) + O(n) ) ~ (n)

QuickSilver
  • 3,915
  • 2
  • 13
  • 29
0

You have two return statements:

 if(ArrayX.length!=ArrayY.length)// to check that they have the same size
   return false;

and

   if (count==ArrayX.length) //to check that number of identical elements is equal to the size 
       return true;
   }

both are conditionally, you need something which gets returned in case no of these if statements are met!

If the count does not match up you probably want to add a return false; because they definitely do not match.

Also see this Answer.

Nordiii
  • 446
  • 1
  • 3
  • 10
  • thank you that removed the error but for some reason when i compared int [] a={2,3,5,6} and int []b={3,2,6,6}; it returned true which is obviuosly false, any idea why that would happen> – maryam p. Jun 30 '20 at 16:05
  • Yeah you're counting the 6 twice! a[3] = 6 a[3] == b[2] count++; a[3] == b[3] count++; You need to remove used values in b[] (inner for loop) and break the inner loop if you hit a a[i] == b[j] because of duplicates. – Nordiii Jun 30 '20 at 17:03
0

Check if any of the arrays are null or not the same length, then loop-over them to check if any equals and increment the counter and break the inner-loop.

    public static boolean isSame(int[] arrayX, int[] arrayY) {
        if (arrayX == null || arrayY == null || arrayX.length != arrayY.length)
            return false;

        int count = 0;
        int len = arrayX.length;

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if (arrayY[j] == arrayX[i]) {
                    count++;
                    break;
                }
            }
        }
        return count == len ? true : false;
    }

, main function

    public static void main(String[] args) {
        System.out.println(isSame(new int[] { 4, 5, 6, 7, 9 }, new int[] { 9, 6, 7, 4, 5 }));
    }

, output

true
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
0

Try this. Convert arrays into Hashmaps that of the value vs how many times the value tursn up in the array. Then compare the maps. I think it's O(n) time.

public static void main(String[] args)
{
    int[] myArray =  {1,2,3,4,5,6,7,8,9,8,7};
    int[] myArray2 =  {6,7,8,9,8,1,2,3,4,5,7};
    HashMap<Integer, Integer> arrayMap = convertArraytoMap(myArray);
    HashMap<Integer, Integer> arrayMap2 = convertArraytoMap(myArray2);


    boolean answer = compareMaps(arrayMap,arrayMap2);
    System.out.println(answer);
    

}//main

//-------------------------------------------------------------------------//


private static HashMap<Integer, Integer> convertArraytoMap(int[] array){
    
    //Map of integer entry versus how many times it turns up
    HashMap<Integer, Integer> arrayMap = new HashMap<Integer, Integer>();
 
    for(int item : array){
        if(arrayMap.containsKey(item))
            arrayMap.put(item, arrayMap.get(item)+1);
        else
        arrayMap.put(item, 1) ;
        
    }//for
    
    return arrayMap;
    
}//convertArraytoMap


//-------------------------------------------------------------------------//

private static boolean compareMaps(   HashMap<Integer, Integer> arrayMap1,    HashMap<Integer, Integer> arrayMap2){
   
     for (int key : arrayMap1.keySet()) {
        
        //Does the 2nd array contain some number of this key
        if(!arrayMap2.containsKey(key))
            return false;
            
        //Does the 2nd array contain the exact same number of this key
        if(arrayMap1.get(key) != arrayMap2.get(key))
            return false;
            
            
    }//for
    
    return true;

}//compareMaps
ShanieMoonlight
  • 1,623
  • 3
  • 17
  • 28