compare arrays of two different lengths

16,254

Solution 1

You could try something like this:

  List similarities = new ArrayList();
  for(int i = 0; i < Math.max(gest_1.length, gest_2.length); i++){
    if (gest_1[i] == gest_2[i])
       similarities.add(gest_1[i];
  }

Solution 2

Use a HashSet. For the union of the two lists,

HashSet<Integer> hashSet = new HashSet<>(); // Contains the union
for(int i = 0; i < array1.length; i++)
    hashSet.add(array1[i]);
for(int i = 0; i < array2.length; i++)
    hashSet.add(array2[i]);

For the intersection of the two lists,

HashSet<Integer> hashSet = new HashSet<>();
List<Integer> list = new ArrayList<>();  // Contains the intersection
for(int i = 0; i < array1.length; i++)
    hashSet.add(array1[i]);
for(int i = 0; i < array2.length; i++) {
    if(hashSet.contains(array2[i])) {
        list.add(array2[i]);
    }
}

Solution 3

Try this function it return array:-

public static String[] numSame (String[] list1, String[] list2) 
     {  
          int same = 0;  
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    same++;  
                    break;  
                }  
             }  
          }  

          String [] array=new String[same];
          int p=0;
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    array[p]=  list1[i]+"";
                    System.out.println("array[p] => "+array[p]);
                    p++;
                    break;  
                }  
             }  
          } 
          return array;
       }  
Share:
16,254
Rachel Fong
Author by

Rachel Fong

iOS engineer. Passionate learner. Fitness junkie.

Updated on June 22, 2022

Comments

  • Rachel Fong
    Rachel Fong almost 2 years

    I am developing a program on Android that will compare the similarity of Gestures using Gesture Points. I have two arrays like this:

    gest_1 = [120,333,453,564,234,531]
    gest_2 = [222,432,11,234,223,344,534,523,432,234]
    

    I know there is no way to dynamically resize either one of the arrays, so is there any way for me to compare both these gestures using these arrays and return the similarity?

    Note that the data in the arrays are just randomly typed out.

  • Rachel Fong
    Rachel Fong about 11 years
    I think this is roughly the idea I am looking for. I don't need the values to be displayed anywhere. If values are almost the same, I will just add one count to a variable or something.
  • Nathan Tuggy
    Nathan Tuggy about 9 years
    It looks as though you're breaking out of the loop after comparing the first element in array2 to its counterpart in array1, except when array2 is only 1 element long. I don't get what the length check against j+ is even for here.