どのように倍以上発生した重複した数字を削除しますが、最初の2回出てくる保つために

MCO:

私は二回が、秩序を保ち、最初の2つの出現よりも多く発生している番号のリストを削除したいJavaでこの問題に出くわしたので。

例えば、リストであれば2、3、5、4、5、2、4、3、5、2、4、4、2、10

予想される出力は、2、3、5、4、5、2、4、3、10あろう

私が出現し、フィルタそれをを追跡するためにカウンタint型を使用してを含むいくつかの方法を、試してみたが、私はそれについて移動する方法を確認していません

class DeDup {
    // function to find the element occurring more than 3 times
    static void get2Occurrences(int arr[]) {
        int i;
        int count = 0;
        for (i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (arr[i] == arr[j])
                    count++;
            }
            if (count < 3 && count > 0) {
                //return arr[i];
                System.out.print(arr[i] + ", ");
            } else {
                for (int k = 2; k > 0; k--) {
                    System.out.print(arr[i] + ", ");
                }
            }
        }
    }
    // driver code
    public static void main(String[]args) {
        int arr[] = new int[]{ 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2, 10 }; 
        //expected output: 2, 3, 5, 4, 5, 2, 4, 3, 10
        //int n = arr.length;
        get2Occurrences(arr);
    }
}

予想される出力は、2、3、5、4、5、2、4、3、10あろう

しかし、私は2、2、3、3、5、5、4、4、5、5、2、2、4、4、3、3、5、5、2、2、4、4、4、4を得ました、2、2、10、10、

イリヤ:

このソリューションでは、他の3つの繰り返し回数を変更することができます。

int[] arr = new int[]{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2, 10};
Map<Integer, Integer> counting = new HashMap<>();
for (Integer current : arr) {
    int count = counting.compute(current, (x, n) -> n == null ? 1 : n + 1);
    if (count < 3) {
        System.out.print(current + ", ");
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=118320&siteId=1