Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
¿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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!