delete repeated values array java

2

I am trying to print an Array without repeating, ie I have:

ja={1,2,3,3,4,5,6,6,8,9};

and the result will be:

jaresul={1,2,3,4,5,6,8,9}

Ideas?

    
asked by anonymous 29.04.2014 / 21:25

5 answers

3

Following is a direct implementation, without using the Collections API, which results in an unrepeatable vector:

import java.util.Arrays;

public class TestIntArray {

    public static void main( String[ ] args ) {
        int[ ] original = { 1 , 3 , 5 , 7 , 9 , 5 , 3 };

        // remover repetidos
        int[ ] unicos = new int[ original.length ];
        int qtd = 0;
        for( int i = 0 ; i < original.length ; i++ ) {
            boolean existe = false;
            for( int j = 0 ; j < qtd ; j++ ) {
                if( unicos[ j ] == original[ i ] ) {
                    existe = true;
                    break;
                }
            }
            if( !existe ) {
                unicos[ qtd++ ] = original[ i ];
            }
        }

        // ajuste do tamanho do vetor resultante
        unicos = Arrays.copyOf( unicos , qtd );

        // imprime resultado
        for( int i = 0 ; i < unicos.length ; i++ ) {
            System.out.println( "" + i + " = " + unicos[ i ] );
        }

    }
}

You can adapt this in a method that receives a int[] and returns a int[] .

    
29.04.2014 / 22:31
6

You can add all in a Set and then print the object:

int[] ja={1,2,3,3,4,5,6,6,8,9};
Set<Integer> set = new HashSet<>();
for(int a: ja) {
    set.add(a);
}
System.out.println(set);

The above code prints:

  

[1, 2, 3, 4, 5, 6, 8, 9]

The Set is a collection of unique objects, so when you add repeated elements to it, those elements are automatically dropped.

Alternative

An alternative to make your code simpler is to use a Integer vector instead of a int vector, so that you can add all elements of the vector to your set variable with just one command, your code would look like this:

Integer[] ja={1,2,3,3,4,5,6,6,8,9};
Set<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(ja));
System.out.println(set);

The result is the same.

The difference is that in the first code the compiler does an Autoboxing of its primitive value for object of its variable set when using the method add() , however it is not possible to use the method Arrays.asList() with a vector of% since it converts its vector to a int , since List<int[]> is considered an object, and also because there is no Autoboxing from int[] to int[] .

Returning to vector

To return the value of Integer[] to vector just do:

Integer[] jb = set.toArray(new Integer[set.size()]);

If you need to transform to vector of set it will have to be something more manual, like:

int cont = 0;
int[] jaresul = new int[ja.length];
for (Integer i : jb) {
    jaresul[cont++] = i;
}

Your int will contain the elements of jaresul without duplicates.

References: Set (Java Platform SE7) ; Arrays.asList (Java Platform SE7)

    
29.04.2014 / 21:30
1

With Java8:

Integer[] ja = {1,2,3,3,4,5,6,6,8,9};
List<Integer> distinctList = Arrays.asList(ja).stream().distinct().collect(Collectors.toList());
    
31.08.2016 / 03:13
0

With While to search and For to print ...

        int[] ja = {1, 2, 3, 3, 4, 5, 6, 6, 8, 9,9,7};
        Arrays.sort(ja);
        int[] jj = new int[ja.length];
        int i = 0;
        int j = 0;     
        int x = 0;
        boolean find;
        while (i < ja.length) { 
            if (i == 0){
                jj[j] = ja[i];
                j++;
            } else {
                x = 0;
                find = false;
                while (x < j && find == false){
                    if (jj[x] == ja[i]) { find = true; }                    
                    x++;
                }
                if (find == false){
                    jj[j] = ja[i];
                    j++;
                }
            }
            i++;
        }            
        ja = Arrays.copyOf(jj, j);            
        for(int r : ja){
            System.out.println(r);
        }
    
30.04.2014 / 00:03
0

This is your solution.

Greetings from Mexico.

int[] ja = {1, 2, 3, 3, 4, 5, 6, 6, 8, 9};
int[] res = Arrays.stream(ja).distinct().toArray();
for (int i = 0; i < res.length; i++) {
    System.out.print(res[i]);
}
    
06.12.2017 / 16:42