【问题标题】:Write Method that returns a List of the numbers from a specified int array that appear n OR more times in that arrayWrite 方法返回指定 int 数组中出现 n 次或多次的数字列表
【发布时间】:2021-11-19 13:55:50
【问题描述】:

我的任务是编写一个方法,该方法从指定的 int 数组中返回一个数字列表,这些数字在该数组中出现 n 次或多次。

例如 findNumbersWithCount(new int[] {5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5 }, 2) 应该找到指定数组中出现 2 次或更多次的所有数字并返回它们的列表,即这个示例调用应该返回一个带有数字 [2,3,4,5]。 也就是说在这个示例数组中,元素 1 出现了一次,2 出现了两次,3 出现了 3 次, 4 四次和 5 五次,使出现两次或多次的元素成为 2、3、4 和 5。 下面是我写的代码:

public class Problem{
  public static List<Integer> findNumbersWithCount(int[] listOfNumbers, int countOfOccurrenceThreshold) {
    int count = 0;

    int [] arr = new int [listOfNumbers.length];
    int visited = -1;  

    for (int i = 0; i < listOfNumbers.length; i++){
        for (int j = i + 1; j < listOfNumbers.length; j++){
            if (listOfNumbers[i] == listOfNumbers[j]){
                count++;

                arr[j] = visited;                    
            }
        }

        if (arr[i] != visited){
           arr[i] = count;
        }
    }

   //calling the generic function that converts Array into List
   List<Integer> list = ArrayToListConversion(arr);

    return list;
}

public static <T> List<T> ArrayToListConversion(int[] array) {
    //creating the constructor of the List class
    List<Integer> list = new ArrayList<>();
    //using for-each loop to iterate over the array
    for (int t : array) {
        //adding each element to the List
        list.add(t);
    }
    //returns the list converted into Array
    return (List<T>) list;
}

public static void main(String[] args){
    try {
        List<Integer> x = findNumbersWithCount(new int[] { 5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5 }, 2);
        System.out.println(x);
    }
    catch (UnsupportedOperationException ex){
        System.out.println("Waiting to be implemented.");
    }
  }
}

我有这个列表 [4, 7, 9, 10, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]而不是 [2,3,4,5]。有人可以帮忙解决这个问题吗?

【问题讨论】:

  • 一个(但可能不是唯一的)错误:您没有在用于计算找到数字的频率的循环之间重置计数器。

标签: java arrays list


【解决方案1】:

这不是一个理想的解决方案,但可能会帮助您了解如何解决它。这是从 C# 转换而来的,如果有些东西看起来不正统,很抱歉。

import java.util.*;

public class Program
{
    public static void main(String[] args)
    {
        java.lang.Iterable<Integer> numbers = FindNumbersWithCount(new int[] {5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5}, 2);
        System.out.println(numbers);
    }

    public static java.lang.Iterable<Integer> FindNumbersWithCount(int[] listOfNumbers, int count)
    {
        HashMap<Integer, Integer> dictionary = new HashMap<Integer, Integer>();
        HashSet<Integer> match = new HashSet<Integer>();

        for (int index = 0; index < listOfNumbers.length; index++)
        {
            int number = listOfNumbers[index];

            if (match.contains(number))
            {
                continue;
            }

            if (!dictionary.containsKey(number))
            {
                dictionary.put(number, 0);
            }

            dictionary.put(number, dictionary.get(number) + 1);

            if (dictionary.get(number).compareTo(count) >= 0)
            {
                match.add(number);
            }
        }

        return match;
    }
}

基本上创建一个数字及其计数的 Map,然后将任何等于或高于所需计数的值保存到 HashSet 并返回。

【讨论】:

  • 感谢您的回答。我试图遵循相同的逻辑,我的答案与预期的略有相同。
【解决方案2】:

肯定不是最好的解决方案:)

public static List<Integer> findNumbersWithCount(int[] listOfNumbers, int countOfOccurrenceThreshold) {
      HashSet<Integer> existing = new HashSet<>();
      return Arrays.stream(listOfNumbers).filter(e -> !existing.add(e)).distinct().sorted().boxed().collect(Collectors.toList());
    }

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多