Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...
- public class MainActivity extends AppCompatActivity {
- private EditText txtEntrada;
- private Button btVerificar;
- private TextView txtResultado;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /*
- * Escreva uma função que receba um array de inteiros positivos com
- * valores de 1 a 5000 e o tamanho do array. A função retorna o
- * número que mais frequentemente aparece no array. Caso hajam dois
- * ou mais números que apareçam à mesma quantidade de vezes, a
- * função deverá retornar o menor número entre os que mais aparecem.
- */
- txtEntrada = (EditText) findViewById(R.id.txtEntradaId);
- btVerificar = (Button) findViewById(R.id.btVerificarId);
- txtResultado = (TextView) findViewById(R.id.txtResultadoId);
- btVerificar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /*
- * Valor usado como entrada para teste no emulador (sem áspas, claro):
- * "[23, 16, 23, 12, 36, 12, 15], 7"
- */
- String txtEntStr = String.valueOf(txtEntrada.getText());
- String[] parts = txtEntStr.split("],");
- parts[0] = parts[0].replace("[", "");
- String[] strArr = parts[0].replace(" ", "").split(",");
- String tamOrig = parts[1];
- parts = null;
- int arrSize = strArr.length;
- Map<String, Integer> repMap = new HashMap<String, Integer>();
- for(int i = 0; i < arrSize; i++)
- {
- if(repMap.containsKey(strArr[i]))
- {
- int mapVal = repMap.get(strArr[i]) + 1;
- repMap.put(strArr[i], mapVal);
- }
- else
- {
- repMap.put(strArr[i], 1);
- }
- }
- // int rMapSize = repMap.size();
- // int[] intArrRep = new int[rMapSize];
- // int cntRep = 0;
- int intMinVal = Integer.MAX_VALUE;
- // for(String key : repMap.keySet())
- // {
- // if(repMap.get(key) >= 2)
- // {
- // intArrRep[cntRep] = Integer.parseInt(key);
- // cntRep++;
- // }
- // }
- //
- // int intMinVal = Integer.MAX_VALUE;
- // for(int i = 0; i < intArrRep.length; i++)
- // {
- // if(intArrRep[i] != 0 && intMinVal > intArrRep[i])
- // {
- // intMinVal = intArrRep[i];
- // }
- // }
- for(String key : repMap.keySet())
- {
- if(repMap.get(key) >= 2)
- {
- int valor = Integer.parseInt(key);
- intMinVal = (valor != 0 && intMinVal > valor) ? valor : intMinVal;
- }
- }
- txtResultado.setText("Menor valor repetido no array: " + intMinVal + " - Tamanho do array original: " + tamOrig);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement