compare arrays of two different lengths

北战南征 提交于 2019-12-06 06:46:14

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];
  }

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]);
    }
}

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;
       }  
        int temp = 0;
        int[] gest_1 = {120, 333, 453, 564, 234, 531};
        int[] gest_2 = {222, 432, 11, 234, 223, 344, 534, 523, 432, 234};
        ArrayList<Integer> g1 = new ArrayList<>();
        ArrayList<Integer> g2 = new ArrayList<>();

        for (int i : gest_1) {
            g1.add(i);
        }
        for (int i : gest_2) {
            g2.add(i);
        }
        for (int i : gest_1) {
            if (g2.contains(i)) {
                temp++;
            }
//            else{
//                break;
//            }
        }

        System.out.println(temp + " element(s) are equal ...");
    }

Does space matter? If not, you could store one of the arrays in a hashtable, and then iterate through the other array checking if the element is contained in the hashtable. This would be O(n) as opposed to O(nm), but this would also add to the size of the algorithm.

If you are unable to do such a thing, it would require two loops. The outer loop would increment the index of the first array after the inner loop has incremented through the entire second array checking if the elements are equal along the way. This could potentially be O(nm).

The above thoughts are assuming that when you say "similarities" it means that there are any elements in one array equal to any of the other elements in the other array.

We consider the two arrays like this
int[] array1={3,5,4,2,6,1,7,9,8}; int[] array2={1,2,3,4,8};

Our aim is find the similar values.

    int[] res;

    if(array1.length>array2.length){
         res=new int[array2.length];
    }else{
        res=new int[array1.length];
    }

      int k=0;

    for(int i=0;i<array1.length;i++)
            {
            for(int j=0;j<array2.length;j++)
                {
                    if(array1[i]==(array2[j]))
                        {
                        res[k]=array1[i];
                            k++;
                            break;

                       }

                }
            }

    for(int l=0;l<res.length;l++){


        System.out.print(res[l]);

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!