Skip to content

Instantly share code, notes, and snippets.

@HectorSaldes
Last active May 4, 2022 16:29
Show Gist options
  • Save HectorSaldes/2dfed06359ef89a5502b21b41ed3e415 to your computer and use it in GitHub Desktop.
Save HectorSaldes/2dfed06359ef89a5502b21b41ed3e415 to your computer and use it in GitHub Desktop.
Desarrollo web integral | Problema 3
package Problema3;
import java.util.Arrays;
/*
- Saldaña Espinoza Hector
- Desarrollo web integral
- 9B
- Problema 3: Dada una matriz de cadenas con nombres de archivos verificar que éstos no se repitan.
En caso de que existir nombres duplicados se deberán renombrar agregando (x) al final,
donde x es el entero más pequeño que aún no ha sido utilizado.
*/
public class MainArchivos {
public static void main(String[] args) {
// String[] nombres = {"txt", "txt", "image", "txt"};
// String[] nombres = {"file", "file", "video", "video"};
String[] nombres = {"b", "b", "b", "b", "img", "b", "img"};
System.out.println(Arrays.toString(renombrar(nombres)));
}
public static String[] renombrar(String[] nombres) {
String[] renombrados = new String[nombres.length];
String[] unicos = Arrays.stream(nombres).distinct().toArray(String[]::new);
int[] contadores = new int[unicos.length];
for (int i = 0; i < unicos.length; i++)
for (int j = 0; j < nombres.length; j++)
if (unicos[i].equals(nombres[j])) {
renombrados[j] = (contadores[i] == 0) ? nombres[j] : nombres[j] + "(" + contadores[i] + ")";
contadores[i]++;
}
return renombrados;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment