Repeated numbers in an array list returning a minimum value and a value of true or false

0

Write a function or method that detects if a number is repeated in an array, at least a certain number of times. The function must receive the arrangement, the number to be detected and the number of minimum times that must appear.

Example:

  

For arrangement = [4, 5, 2, 4, 5, 9, 9, 4, 4]

contiene(arreglo, 4, 5) // Regresa false;
contiene(arreglo, 4, 4) // Regresa true;
contiene(arreglo, 4, 3) // Regresa true;
contiene(arreglo, 9, 2) // Regresa true;

This is the code of what I tried:

  public static void main(String[] args) {
    int[] array = {1, 2, 2, 3, 6, 4, 4, 4, 9, 5, 6, 5, 9, 2};
    list = new int[array.length];
    for (int i = 0; i < array.length; i++) {
      int count = 0;
      for (int j = 0; j < array.length; j++) {
        if (array[i] == array[j]) {
          count++;
          if (numero(array[i])) {
            list[i] = array[i]; 
          } 
        } 
      } 
      if (list[i] != 0) {
        System.out.println(list[i] + " it has been found " + count + " times"); 
      } 
    } 
  }

public class DuplicatedDataInArray { 
   static int[] list; 
   static boolean numero(int num) { 
     for (int i = 0; i < list.length; i++) {
       if (list[i] == num) {
         return false; 
       } 
     } 
     return true; 
   } 
}
    
asked by Specter 15.09.2018 в 17:32
source

1 answer

0

Friend, what you were not doing was instantiating the class that has the variable and the method you need in MAIN :

public static void main(String[] args) {
  int[] array = {1, 2, 2, 3, 6, 4, 4, 4, 9, 5, 6, 5, 9, 2};
  DuplicatedDataInArray.list = new int[array.length];
  for (int i = 0; i < array.length; i++) {
    int count = 0;
    for (int j = 0; j < array.length; j++) {
      if (array[i] == array[j]) {
        count++;
        if (DuplicatedDataInArray.numero(array[i])) {
          DuplicatedDataInArray.list[i] = array[i];
        }
      }
    }
    if (DuplicatedDataInArray.list[i] != 0) {
      System.out.println(DuplicatedDataInArray.list[i] + " it has been found " + count + " times");
    }
  }
}

public class DuplicatedDataInArray { 
   static int[] list; 
   static boolean numero(int num) { 
     for (int i = 0; i < list.length; i++) {
       if (list[i] == num) {
         return false; 
       } 
     } 
     return true; 
   } 
}
    
answered by 15.09.2018 в 18:05