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?
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?
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[]
.
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.
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 variableset
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[]
.
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)
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());
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);
}
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]);
}