zoukankan      html  css  js  c++  java
  • 查找两个数组的相同元素

    public static Set<Integer> getIds(Integer[] a, Integer[] b){
          
          Set<Integer> same = new HashSet<Integer>();  //用来存放两个数组中相同的元素
          Set<Integer> temp = new HashSet<Integer>();  //用来存放数组a中的元素
          
          for (int i = 0; i < a.length; i++) {
              temp.add(a[i]);   //把数组a中的元素放到Set中,可以去除重复的元素
          }
          
          for (int j = 0; j < b.length; j++) {
            //把数组b中的元素添加到temp中
            //如果temp中已存在相同的元素,则temp.add(b[j])返回false
            if(!temp.add(b[j]))
                same.add(b[j]);
        }
        return same;
      }


      public static void main(String[] arg){
            Integer[] array1 = {1,2,3,4,1,2,4,6,7,8,10,22,33};
            
            Integer[] array2 = {1,2,3,4,1,2,4,6,7,8,10,22,33,55,66,77,88,99};
            
            Set<Integer> sameElementSet = getIds(array1,array2);
            
            for(Integer i : sameElementSet) {
            
            System.out.println(i);
            
            }
      }

  • 相关阅读:
    NOIP 模拟 序列操作
    LUOGU 1525 关押罪犯
    HDU2473 Junk-Mail Filter
    BZOJ 2096 Pilots
    luogu 3939 数颜色
    NOIP模拟 赌博游戏
    Unity3D
    HTML5
    Cocos2d-x——支持多触点
    Cocos2d-x——Cocos2d-x 屏幕适配总结
  • 原文地址:https://www.cnblogs.com/JeffXia/p/7015608.html
Copyright © 2011-2022 走看看