Home > database >  Write Method that returns a List of the numbers from a specified int array that appear n OR more tim
Write Method that returns a List of the numbers from a specified int array that appear n OR more tim

Time:09-27

I have this task of Write a method that returns a List of the numbers from a specified int array that appear n OR more times in that array.

For example, findNumbersWithCount(new int[] {5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5 }, 2) should find all the numbers in the specified array that appear 2 OR more times and return a list of them i.e. this example call should return a list with the numbers [2,3,4,5]. That is to say in this example array, element 1 occurs once, 2 twice, 3 three times, 4 four times, and 5 five times, making elements that appear two OR more times 2,3,4 and 5. Below is the code I wrote:

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.");
    }
  }
}

I'm having this list [4, 7, 9, 10, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] instead of [2,3,4,5]. Could anyone please help solve this issue ?

CodePudding user response:

For sure not the best solution :)

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());
    }

CodePudding user response:

This is not an ideal solution, but might help you understand how to solve it. This is converted from C# so sorry if something looks unorthodox.

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;
    }
}

Basically create a Map of the numbers and their counts, then save any that is equal or above the desired count to a HashSet and return it.

  • Related