Advertisement
lencinasalejo

Ejercicio03

Apr 3rd, 2022 (edited)
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.TreeMap;
  3.  
  4. public class Ejercicio03 {
  5.    
  6.     public Map<Integer, Integer> contarAparicionesDeLosValores(int fila[]){
  7.         int elementosDeLaFila = fila.length;
  8.        
  9.         //Uso un mapa que como clave tiene el numero y como valor tiene la
  10.         //cantidad de veces que este numero se repite en la fila
  11.        
  12.         Map<Integer, Integer> numeros = new TreeMap<Integer, Integer>();
  13.         for(int i=0; i<elementosDeLaFila; i++) {
  14.            
  15.             if(numeros.containsKey(fila[i])) {
  16.                
  17.                 //Si el numero ya esta mapeado le sumo 1
  18.                
  19.                 int contNumero = numeros.get(fila[i]);
  20.                 contNumero++;
  21.                 numeros.replace(fila[i], contNumero);
  22.             }
  23.             else {
  24.                
  25.                 //Si el numero no esta mapeado inicializo su contador en 1
  26.                
  27.                 numeros.put(fila[i], 1);
  28.             }
  29.            
  30.         }
  31.        
  32.         return numeros;
  33.     }
  34.    
  35.     public int calcularModa(Map<Integer, Integer> numeros) {
  36.        
  37.         //Recorro el map para ver de los numeros que mas veces
  38.         //se repiten cual es el mayor
  39.        
  40.         int mayorVeces = 0, moda = 0;
  41.        
  42.         for (Map.Entry<Integer, Integer> numero : numeros.entrySet()) {
  43.             if(numero.getValue()>mayorVeces) {
  44.                 mayorVeces = numero.getValue();
  45.                 moda = numero.getKey();
  46.                 }
  47.                 else if(numero.getValue()==mayorVeces){
  48.                     if(numero.getKey()>moda) {
  49.                         moda = numero.getKey();
  50.                     }
  51.                 }
  52.             }
  53.        
  54.         return moda;
  55.     }
  56.    
  57.     public int[] modasDeCadaFila(int matriz[][]) {
  58.         int cantFilas = matriz.length;
  59.         int vectorModas[] = new int[cantFilas];
  60.        
  61.         for(int i=0; i<cantFilas; i++) {
  62.             Map<Integer, Integer> numeros = contarAparicionesDeLosValores(matriz[i]);
  63.             vectorModas[i] = calcularModa(numeros);
  64.         }
  65.        
  66.         return vectorModas;
  67.     }
  68.    
  69.     public void imprimirVector(int[] vector) {
  70.         for (int i = 0; i < vector.length; i++) {
  71.             System.out.print(vector[i] + " ");
  72.         }
  73.     }
  74.    
  75.     public static void main(String[] args) {
  76.         int[][] matriz = {
  77.                 {1,2,3,4},
  78.                 {5,-6,-6,20},
  79.                 {1,1,10,10}};
  80.        
  81.         int vectorModas[] = new int[matriz.length];
  82.        
  83.         Ejercicio03 eje = new Ejercicio03();
  84.        
  85.         vectorModas = eje.modasDeCadaFila(matriz);
  86.        
  87.         eje.imprimirVector(vectorModas);
  88.        
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement