Skip to content

Commit 5c4329f

Browse files
committed
Added Actividad7 - Unit 5
1 parent 59bcebf commit 5c4329f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package unit05.Actividad07;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
Implementar la función: int[] sinRepetidos(int[] t) que construye y devuelve una tabla
7+
de la longitud apropiada, con los elementos de t, donde se han eliminado los elementos repetidos.
8+
*/
9+
public class Actividad7 {
10+
public static void main(String[] args) {
11+
int[] t = {1,2,6,32,5,246,4573,43,15,1346,24,6,346,14,5};
12+
13+
int[] sinRepetidos = sinRepetidos(t);
14+
15+
for (int sinRepetido : sinRepetidos) {
16+
System.out.println(sinRepetido);
17+
}
18+
}
19+
20+
private static int[] sinRepetidos(int[] t) {
21+
int[] sinRepetidos = new int[0];
22+
23+
for (int j : t) {
24+
int indice = Arrays.binarySearch(sinRepetidos, j);
25+
if (indice <= -1) {
26+
sinRepetidos = Arrays.copyOf(sinRepetidos, sinRepetidos.length + 1);
27+
sinRepetidos[sinRepetidos.length - 1] = j;
28+
Arrays.sort(sinRepetidos);
29+
}
30+
}
31+
return sinRepetidos;
32+
}
33+
}

0 commit comments

Comments
 (0)