Advertisement
Victoralm

Verifica menor valor repetido em um array

Nov 24th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. ...
  2.  
  3. public class MainActivity extends AppCompatActivity {
  4.  
  5.     private EditText txtEntrada;
  6.     private Button btVerificar;
  7.     private TextView txtResultado;
  8.  
  9.  
  10.  
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.  
  16.  
  17.         /*
  18.          * Escreva uma função que receba um array de inteiros positivos com
  19.          * valores de 1 a 5000 e o tamanho do array. A função retorna o
  20.          * número que mais frequentemente aparece no array. Caso hajam dois
  21.          * ou mais números que apareçam à mesma quantidade de vezes, a
  22.          * função deverá retornar o menor número entre os que mais aparecem.
  23.          */
  24.  
  25.         txtEntrada = (EditText) findViewById(R.id.txtEntradaId);
  26.         btVerificar = (Button) findViewById(R.id.btVerificarId);
  27.         txtResultado = (TextView) findViewById(R.id.txtResultadoId);
  28.  
  29.         btVerificar.setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View v) {
  32.  
  33.              /*
  34.              * Valor usado como entrada para teste no emulador (sem áspas, claro):
  35.              * "[23, 16, 23, 12, 36, 12, 15], 7"
  36.              */
  37.  
  38.  
  39.             String txtEntStr = String.valueOf(txtEntrada.getText());
  40.  
  41.             String[] parts = txtEntStr.split("],");
  42.             parts[0] = parts[0].replace("[", "");
  43.             String[] strArr = parts[0].replace(" ", "").split(",");
  44.             String tamOrig = parts[1];
  45.             parts = null;
  46.             int arrSize = strArr.length;
  47.  
  48.             Map<String, Integer> repMap = new HashMap<String, Integer>();
  49.  
  50.             for(int i = 0; i < arrSize; i++)
  51.             {
  52.                 if(repMap.containsKey(strArr[i]))
  53.                 {
  54.                     int mapVal = repMap.get(strArr[i]) + 1;
  55.                     repMap.put(strArr[i], mapVal);
  56.                 }
  57.                 else
  58.                 {
  59.                     repMap.put(strArr[i], 1);
  60.                 }
  61.             }
  62.  
  63. //            int rMapSize = repMap.size();
  64. //            int[] intArrRep = new int[rMapSize];
  65. //            int cntRep = 0;
  66.             int intMinVal = Integer.MAX_VALUE;
  67. //            for(String key : repMap.keySet())
  68. //            {
  69. //                if(repMap.get(key) >= 2)
  70. //                {
  71. //                    intArrRep[cntRep] = Integer.parseInt(key);
  72. //                    cntRep++;
  73. //                }
  74. //            }
  75. //
  76. //            int intMinVal = Integer.MAX_VALUE;
  77. //            for(int i = 0; i < intArrRep.length; i++)
  78. //            {
  79. //                if(intArrRep[i] != 0 && intMinVal > intArrRep[i])
  80. //                {
  81. //                    intMinVal = intArrRep[i];
  82. //                }
  83. //            }
  84.  
  85.             for(String key : repMap.keySet())
  86.             {
  87.                 if(repMap.get(key) >= 2)
  88.                 {
  89.                     int valor = Integer.parseInt(key);
  90.                     intMinVal = (valor != 0 && intMinVal > valor) ? valor : intMinVal;
  91.                 }
  92.             }
  93.  
  94.             txtResultado.setText("Menor valor repetido no array: " + intMinVal + " - Tamanho do array original: " + tamOrig);
  95.  
  96.             }
  97.         });
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement