计算数组交集中唯一元素的数量

时间:2014-01-28 18:16:56

标签: java

当我跑步时,我明白了:

运行assignment1question1

2147483642

它应该返回'2',因为我试图在两个数组的交集中找到唯一元素的数量。请帮忙。 感谢。

public class assignment1question1 {
    public static void main(String[] args) {
        int[] a = {1,3,2,3,5};
        int[] b = {1,3,4,1,7,3};
        int n = 5;
        int m = 6;
        System.out.print(listIntersection(a,b,n,m));
    }
    public static int listIntersection (int[] a, int[] b, int n, int m) {
        int i,j,k;
        int intersect = 0;

        for(i=0; i<n; i++) {
            int duplicate = 0;

            for(j=0; j<=i; j++) {
                if(a[i] == a[j]) {
                    duplicate = duplicate + 1;
                }
            }
            if(duplicate == 1) {
                for(k=0; k<m; m++) {
                    if(a[i] == b[k]) {
                        intersect = intersect + 1;
                    }
                }
            }
        }
    return intersect;  
    }
}

这是我更新的代码:

    public class assignment1question1 {
     public static void main(String[] args) {
     int[] a = {1,3,2,3,5};
     int[] b = {1,3,4,1,7,3};
     int n = a.length;
     int m = b.length;
     System.out.print(listIntersection(a,b,n,m));
     }
     public static int listIntersection (int[] a, int[] b, int n, int m) {
      int i,j,k;
      int intersect = 0;

      for(i=0; i<n; i++) {
        int duplicate = 0;

        for(j=1; j<=i; j++) {
          if(a[i] == a[j]) {
          duplicate = duplicate + 1;
        }
        }
        if(duplicate == 0) {
          for(k=0; k<m; k++) {
            if(a[i] == b[k]) {
              intersect = intersect + 1;
              break;
            }
          }
        }
      }
  return intersect;  
  }
  }

4 个答案:

答案 0 :(得分:0)

你被困在无限循环becoz中你将m值增加为m ++:

    if(duplicate == 1) {
                    for(k=0; k<m; k++) {
                        if(a[i] == b[k]) {
                            intersect = intersect + 1;
                        }
                    }
                }

答案 1 :(得分:0)

import java.util.HashSet;
import java.util.Set;

public class Del {
    public static void main(String[] args) {
        int[] a = { 1, 3, 2, 3, 5 };
        int[] b = { 1, 3, 4, 1, 7, 3 };

        // duplicates removed by SET
        Set intersect = new HashSet();

        for (int aVal : a) {
            for (int bVal : b) {
                if (aVal == bVal) {
                    intersect.add(aVal);
                    System.out.println("->" + aVal);
                }
            }
        }
        System.out.println("Size =" + intersect.size());
    }
}

答案 2 :(得分:0)

如果您想让代码正常工作,则需要修复k for-cycle,其中迭代b数组。

            if(duplicate==1) {
                for(k=0; k<m; k++) { // increase k
                    if(a[i] == b[k]) {
                        intersect = intersect + 1;
                        break; // insert break to avoid duplicates in b array
                    }
                }
            }

这里有点清洁和更短的代码。 (我假设,您不能使用Java集合类。)

public class assignment1question1 {

    public static void main(String[] args) {
        int[] a = {1,3,4,3,5};
        int[] b = {1,3,4,1,7,3};
        System.out.print(listIntersection(a,b));
    }

    public static int listIntersection(int[] a, int[] b) {
        int intersect = 0;

        for(int i = 0; i < a.length; i++) {
            boolean duplicate = false;

            for (int j = 0; j < i; j++) {
                if(a[i] == a[j]) {
                    duplicate = true;
                    break;
                }
            }

            if (!duplicate) {
                for(int k = 0; k < b.length; k++) {
                    if (a[i] == b[k]) {
                        intersect++;
                        break;
                    }
                }
            }
        }
        return intersect; 
    }
}

答案 3 :(得分:0)

import java.util.*;

public class HelloWorld {

     public static void main(String []args) {
        int[] a = {1,3,2,3,5};
        int[] b = {1,3,4,1,7,3};
        Set<Integer> aTmp = asSet(a);
        aTmp.retainAll(asSet(b));
        System.out.println(aTmp.size());
     }

    public static Set<Integer> asSet(int... args) {
        Set<Integer> tmp = new HashSet<Integer>();
        for (int i : args) {
            tmp.add(i);
        }
        return tmp;
    }
}