简体   繁体   English

所有号码都被重复少于 N 次

[英]all number have been rebeated less than N times

i'm tring to return an array that have all number have been rebeated less than N times.我试图返回一个所有数字都被重复少于 N 次的数组。

expected output : 1,4预期输出:1,4

this is my cod :这是我的鳕鱼:

-- Main -- - 主要的 -

 int[] data = {1, 2, 2, 3, 3, 3, 4, 5, 5};
    int n = 1;
        Solution.solution(data, n);

-- class Solution -- -- 类解决方案 --

public static int[] solution(int[] data, int n) {
        
        int l =  data.length, count = 0;
        int[] Narr = new int[l];
        
        for(int i =0 ; i < l; i++){
            count = 0;
            
            for(int j=1 ; j < l ; j++){
                if(data[i] == data[j]){
                    count++;
                }
                
                if(j == l-1){
                    
                    if(count < n){
                         Narr[i] = data[i];
                         System.out.println(Narr[i]);
                     } 
                }
            }
            
        }
        
        return Narr;
    }

I would break this down to multiple steps.我会将其分解为多个步骤。

First lets create a countMap that holds track of all the occurrences:首先让我们创建一个countMap来跟踪所有事件:

int[] data = {1, 2, 2, 3, 3, 3, 4, 5, 5};
int n = 1;

Map<Integer, Long> countMap = Arrays.stream(data).boxed()
        .collect(Collectors.groupingBy(s -> s, Collectors.counting()));

System.out.println(countMap); // {1=1, 2=2, 3=3, 4=1, 5=2}

Now all we need to do is to check for the condition count <= n :现在我们需要做的就是检查条件count <= n

List<Integer> resultList = new ArrayList<>();
countMap.forEach((integer, count) -> {
    if (count <= n) resultList.add(integer);
});

System.out.println(resultList); // [1, 4]

And last but not least converting the list back to array :最后但并非最不重要的是将list转换回array

// {1, 4}
int[] resultArray = resultList.stream()
        .mapToInt(Integer::intValue)
        .toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找数组在小于O(n ^ 2)内重复的次数 - Find the number of times a number is repeated in an array in less than O(n^2) 给定数字n,我们必须找出小于或等于n的正好有3个除数的数字 - given a number n , we have to find out such numbers that are less than or equal to n, that have exactly 3 divisors 模式java:2个字符小于n次,它们的总和也小于n - Pattern java : 2 caracters less than n times and their sum of appereance less n too 小于n的平方根的n的因数 - number of factors of n that are less than square root of n 打印出素数小于给定数N - Print out prime Number less than a given number N 如何计算小于n且总和大于n * 2的3个整数的组合数? - How to calculate the number of combinations of 3 integers less than n whose sum is greater than n * 2? 您如何比较已转换为整数的字符串的值(大于/小于)? - How do you compare values (greater than/less than) of strings that have been turned into integers? 删除给定数组中出现次数超过 N 次的所有数字 - Remove all Numbers that occur more than N times in the given Array 确定一个数(n)的因数,然后返回乘以小于(n)的可能的正整数对? - Determine factors of a number (n), then return possible pairs of positive integers that when multiplied together are less than (n)? 如何显示小于给定数字的所有偶数? - How to display all even numbers less than a given number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM