算法:请找出数组a所有重复元素和比较数组a和数组b得到不重复的新数组和比较数组a和数组b请找出所有重复元素

    /**
     * 1.给定数组int[] a,int[] b
     * (1)请找出数组a所有重复元素,例:int[] a = {1,2,3,4,8,9,3,5,1,3},结果int[] a1 = {1,1,3,3,3}
     * (2)比较数组a和数组b得到不重复的新数组,例:int[] a = {1,2,3,4,8,9,3,5,1,3},int[] b = {2,7,6,0,5},结果int[] c = {1,2,3,4,5,6,7,8,9,0}
     *  (3)比较数组a和数组b请找出所有重复元素,例:int[] a = {1,2,3,4,8,9,3,5,1,3},int[] b = {2,7,6,0,5},结果int[] e = {1,1,2,2,3,3,3,5,5}
     *
     */
    @Test
    public void suanfa42()
    {

        int[] a = {1,2,3,4,8,9,3,5,1,3};
        int[] b = {2,7,6,0,5};
        //          {1,3,7,6,0}

        //(1)请找出数组a所有重复元素,例:int[] a = {1,2,3,4,8,9,3,5,1,3},结果int[] a1 = {1,1,3,3,3}
        int[] a1 = this.getMethod1(a);
        System.out.println("a1 = " + a1);

        //(2)比较数组a和数组b得到不重复的新数组,例:int[] a = {1,2,3,4,8,9,3,5,1,3},int[] b = {2,7,6,0,5},结果int[] c = {1,2,3,4,5,6,7,8,9,0}
        int[] c = Stream.of(Arrays.stream(a).boxed(), Arrays.stream(b).boxed()).flatMap(item -> item).distinct()
                .mapToInt(Integer::valueOf).toArray();
        System.out.println("c = " + c);

        // (3)比较数组a和数组b请找出所有重复元素,例:int[] a = {1,2,3,4,8,9,3,5,1,3},int[] b = {2,7,6,0,5},结果int[] e = {1,1,2,2,3,3,3,5,5}
        int[] e = this.getMethod3(a, b);
        System.out.println("e = " + e);
        System.out.println("e = " + e.toString());

    }

    /**
     * (1)请找出数组a所有重复元素,例:int[] a = {1,2,3,4,8,9,3,5,1,3},结果int[] a1 = {1,1,3,3,3}
     *
     * @param a
     * @return
     */
    private int[] getMethod1(int[] a)
    {
        int[] a1 = new int[a.length];
        int index = 0;
        HashMap<Integer, Integer> countMap = new HashMap<>();
        for (int i : a)
        {
            if (countMap.containsKey(i))
            {
                Integer count = countMap.get(i);
                if (count == 1)
                {
                    a1[index++] = i;
                }
                a1[index++] = i;
                countMap.put(i, count + 1);
            }
            else
            {
                countMap.put(i, 1);
            }
        }
        return a1;
    }

    /**
     * (3)比较数组a和数组b请找出所有重复元素,例:int[] a = {1,2,3,4,8,9,3,5,1,3},int[] b = {2,7,6,0,5},结果int[] e = {1,1,2,2,3,3,3,5,5}
     *
     * @param a
     * @param b
     * @return
     */
    private int[] getMethod3(int[] a, int[] b)
    {
        int[] e = new int[a.length + b.length];

        // 合并两个数组并排序
        List<Integer> integers = Stream.of(Arrays.stream(a).boxed(), Arrays.stream(b).boxed())
                .flatMap(item -> item).sorted().collect(Collectors.toList());
        HashMap<Integer, Integer> countMap = new HashMap<>();
        int index = 0;
        for (Integer item : integers)
        {
            if (countMap.containsKey(item))
            {
                Integer count = countMap.get(item);
                if (count == 1)
                {
                    e[index++] = item;
                }
                e[index++] = item;
                countMap.put(item, count+1);

            }
            else
            {
                countMap.put(item, 1);
            }
        }
        return e;
    }

 

posted @ 2024-07-30 18:00  骚哥  阅读(17)  评论(0编辑  收藏  举报