Algorithm for taking elements from an in[] array and concatenating them [closed]

Concatenate them together as a string and then parse the string as an int:

StringBuilder sb = new StringBuilder();
for (int i : array1) {
    sb.append(i);
}
int concatenated = Integer.parseInt(sb.toString());

An alternative (and, to my mind, more cumbersome) approach is to initialize a sum to 0 and then add each array element after multiplying sum by 10 or 100 (or 1000, …) depending on how big the array element is.

Leave a Comment