Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
¿Cómo puedo saber la frecuencia con la que aparece un número en una matriz?

Manejo guardar una lista de números aleatorios en una matriz, ahora tengo una matriz de 7 números: (3,4,6,6,10,3,5), y quiero saber cuál es la frecuencia con la que aparecen los números. .

Lo intenté con esto, pero realmente no sé cómo hacerlo.

 int size = 7 int aux [] = {3,4,6,6,10,3,5}; for(int i = 0 ; i<size; i++) { int cont = 0 ; for(int j = 0; i<size-1; j++) { if(aux[i] == aux[j+1]) { aux[j+1] = -1; cont++; } } System.out.println("The number "+ aux[i] + " appears "+ cont + "times"); }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Podría usar un Mapa y contar las ocurrencias usted mismo en un bucle:

 import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class Main { private static Map<Integer, Integer> getCounts(int[] arr) { Map<Integer, Integer> counts = new HashMap<>(); for (int x : arr) { counts.put(x, counts.getOrDefault(x, 0) + 1); } return counts; } public static void main(String[] args) { int[] arr = {3, 4, 6, 6, 10, 3, 5}; System.out.println(Arrays.toString(arr)); Map<Integer, Integer> counts = getCounts(arr); for (Map.Entry<Integer, Integer> entry : counts.entrySet()) { System.out.printf("%d occurs %d %s\n", entry.getKey(), entry.getValue(), entry.getValue() == 1 ? "time" : "times"); } } }

Producción:

 [3, 4, 6, 6, 10, 3, 5] 3 occurs 2 times 4 occurs 1 time 5 occurs 1 time 6 occurs 2 times 10 occurs 1 time
over 4 years ago · Santiago Trujillo Denunciar

0

cree una estructura de mapa Map<Integer, Integer> con la clave es su elemento, el valor es el momento en que aparece el elemento en la matriz.

 Map<Integer, Integer> map = new HashMap<>(); for (int e : array){ if (map.containsKey(e)){ map.put(e, map.get(e) + 1); } else { map.put(e, 1); } } for (Map.Entry<Integer, Integer> e : map.entrySet()){ System.out.println( e.getKey() + ": " + e.getValue()); }
over 4 years ago · Santiago Trujillo Denunciar

0

Puedes probar con esto:

 import java.util.*; public class ArrayCount { public static void main(String[] args) { int [] a = {3,4,6,6,10,3,5}; int n = a.length; int [] tmp = new int [n]; System.arraycopy(a, 0, tmp, 0, n); int Num = 1; Arrays.sort(tmp); for(int i = 1; i < n; i ++) { if (tmp[i] != tmp[i-1]) { Num ++; } } int [] b1 = new int [Num]; int [] b2 = new int [Num]; for (int i = 0; i < Num; i ++) { b2[i] = 1; } int j = 0; for(int i = 1; i < n; i ++) { if (tmp[i] == tmp[i-1]) { b1[j] = tmp[i]; b2[j] ++; } else { j ++; b1[j] = tmp[i]; } } System.out.println("The number of elements in the array: " + Num); System.out.println("List of different elements: " + b1); System.out.println("List of times of different elements: " + b2); } }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda