Skip to content

Instantly share code, notes, and snippets.

@wedwin53
Created February 15, 2021 16:02
Show Gist options
  • Save wedwin53/c2ad87eeede7538d2fb46250412ddd5c to your computer and use it in GitHub Desktop.
Save wedwin53/c2ad87eeede7538d2fb46250412ddd5c to your computer and use it in GitHub Desktop.
Sort number in an array by given pattern
public class SpringRegistroApplication {
public static void main(String[] args) {
int[] input = {4,9,1,2,3, 6, 5, 8};
int[] pattern = {1,4,5,3,9,2};
List<Integer> result = new ArrayList<>();
Set<Integer> remainder = new HashSet<>();
//valor inicial a evaluar en el patron
for (int i = 0; i < pattern.length; i++) {
//por cada valor del patron comparamos con el input
for (int j = 0; j < input.length; j++) {
if(pattern[i] == input[j]) {
result.add(input[j]);
input=ArrayUtils.remove(input, j);
}else {
remainder.add(input[j]);
}
}//fin for input
}//fin for pattern
System.out.println("-----RESULT ----");
for (Integer res : result) {
System.out.println(res);
}
System.out.println("-----FIN RESULT ----");
System.out.println("");
System.out.println("----- REMAINDER ----");
for (Integer rem : remainder) {
System.out.println(rem);
}
System.out.println("-----FIN REMAINDER ----");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment