Problem to get the cardinal of a set of integers

0

I can not come up with a solution to this problem because the implement the code that I developed returns me the cardinal of the array {1,5,9,5,8,1,3,3,8} is 8 when in fact it should be 5. Many thanks in advance to everyone who offers me their help to solve this problem. The arrays will be size 10 always and the set of integers as well. In addition, the set of integers can not have repeated elements.

public class ConjuntoDeEnteros {

    private int cardinal;
    private int [] elementos;
    public ConjuntoDeEnteros(){
        elementos = new int[10];
        cardinal =0;
    }
    public ConjuntoDeEnteros(int [] array) {
        elementos = new int[10];
        cardinal = 0;

        cardinal = array.length;                    //en el conjunto de enteros no habran elementos repetidos asi que si se le pasa un array {1,5,9,5,8,1,3,3,8} array.length = 9
        elementos = array;                          //el conjunto de enteros seria {1,3,5,8,9} y en vez de cardinal = array.length, el cardinal = 5
}
    public int cardinal () {
        for (int i = 0; i < elementos.length;i++) { //el array siempre tendra 10 elementos pero puede no estar completo
            if(elementos[i] != 0) {                 //en este caso el cardinal sera igual al numero de elementos que haya en el array
                cardinal = i;                       //el cardinal se ira aumentando a medida que se vayan añadiendo los elementos al conjunto de enteros
            }else {
                cardinal = 0;
            }
        }
        return cardinal;
    }
}
    
asked by Daniel 01.11.2017 в 12:26
source

1 answer

0

Taking into account everything that we already talked about, I eliminated my previous answer and now I have this new answer.

The problem is that what you are taking as a cardinal is the number of elements you have in your arrangement, but what you want is the number of elements that are not repeated .

You were on the right track from the beginning. I put the constructor where is the complete logic:

public ConjuntoDeEnteros(final int[] array) {
    // Inicializa el arreglo de elementos
    elementos = new int[10];
    // Variable que nos ayudará para saber si el número ya existe en nuestro arreglo.
    boolean existe;
    // Recorre el arreglo de entrada
    for (int i = 0; i < array.length; i++) {
        // Obten el siguiente número de tu entrada
        final int num = array[i];
        // Resetea nuestra bandera
        existe = false;
        // Recorre el arreglo final
        for (int j = 0; j < cardinal; j++) {
            // Si el número que estamos evaluando
            if (elementos[j] == num) {
                existe = true;
                break;
            }
        }
        // Si no se encontró el número entonces agrégalo a nuestro arreglo final y aumenta el cardinal.
        if (!existe) {
            elementos[cardinal] = num;
            cardinal++;
        }
    }
}

This should be enough for you to finish your work. What is missing are things that you already had in your code.

Let me know if this serves you. I tried to use elements that I suppose you should already know or that will have taught you.

    
answered by 01.11.2017 / 16:59
source