delete repeated values java array

Asked

Viewed 18,470 times

2

I’m trying to print an Array without repeats, which means 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?

  • 4

    The quality of the question is low, try to be more specific and put a little bit about your attempts to solve the problem or search places where the question may have been answered. For the next questions I recommend reading How to Ask a Good Question

5 answers

6

You can add all in one 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 code above prints:

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

Set is a collection of unique objects, so when repeated elements are added to it these elements are automatically discarded.

Alternative

An alternative to make your code simpler is to use a vector of Integer instead of a vector of int, as it is possible to add all elements of the vector to its variable set with only 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 makes an Autoboxing of its primitive value for its variable object set when using the method add(), however it is not possible to use the method Arrays.asList() with a vector of int because it converts its vector into a List<int[]>, for the int[] is considered an object, and also because there is no Autoboxing of int[] for Integer[].

Returning to vector

To return the value of set for vector just do:

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

You need to transform to vector int it has to be something more manual, like:

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

Your jaresul shall contain the elements of jaresul without duplicates.

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

  • has to return int[], hence your solution does not seem to apply.

  • @board11 see if now meets

  • I understand your solution what happens is I have to do obligatorily Return the result, or if with your solution I Return Jb was give error by can not convert from integer[] to int[]

  • @board11 saw my latest edition? just converted back from set to Integer[] and then to int[], I believe that’s what you asked for, or I’m mistaken?

3


Follows a direct implementation, without using the Collections API, which results in a repeatless 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 that to a method that receives a int[] and returns a int[].

  • Brilliant very grateful.

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());

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);
        }

0

This is your solution.

saludos since 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]);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.