# Concatenate the Array of elements into a single element

Last Updated : 06 Dec, 2023

Given an array, arr[] consisting of N integers, the task is to print the single integer value obtained by joining the array elements.

Examples:

Input: arr[] = {1, 23, 345}
Output: 12345

Input: arr[] = {123, 45, 6, 78}
Output: 12345678

## Method 1 :

Approach: The given problem can be solved based on the following observations:

1. Considering X and Y as the two integer values to be joined. And also considering the length of the integer Y as l.
2. Then two integers X and Y can be joined together as following:
•  XÃ—10l +Y

Follow the steps below to solve the problem:

• Initialize a variable, say ans as 0, to store the resulting value.
• Traverse the array arr[] using the variable i, and then in each iteration multiply ans by 10 to the power of the count of the digit in the integer arr[i] and increment ans by arr[i].
• Finally, after the above step, print the answer obtained in ans.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the integer value` `// obtained by joining array elements` `// together` `int` `ConcatenateArr(``int` `arr[], ``int` `N)` `{` `    ``// Stores the resulting integer value` `    ``int` `ans = arr[0];`   `    ``// Traverse the array arr[]` `    ``for` `(``int` `i = 1; i < N; i++) {`   `        ``// Stores the count of digits of` `        ``// arr[i]` `        ``int` `l = ``floor``(``log10``(arr[i]) + 1);`   `        ``// Update ans` `        ``ans = ans * ``pow``(10, l);`   `        ``// Increment ans by arr[i]` `        ``ans += arr[i];` `    ``}` `    ``// Return the ans` `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``// Input` `    ``int` `arr[] = { 1, 23, 456 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``// Function call` `    ``cout << ConcatenateArr(arr, N);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{` `    `  `// Function to find the integer value` `// obtained by joining array elements` `// together` `static` `int` `ConcatenateArr(``int``[] arr, ``int` `N)` `{` `    `  `    ``// Stores the resulting integer value` `    ``int` `ans = arr[``0``];`   `    ``// Traverse the array arr[]` `    ``for``(``int` `i = ``1``; i < N; i++)` `    ``{` `        `  `        ``// Stores the count of digits of` `        ``// arr[i]` `        ``int` `l = (``int``)Math.floor(Math.log10(arr[i]) + ``1``);`   `        ``// Update ans` `        ``ans = ans * (``int``)Math.pow(``10``, l);`   `        ``// Increment ans by arr[i]` `        ``ans += arr[i];` `    ``}` `    `  `    ``// Return the ans` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `main(String args[])` `{` `    `  `    ``// Input` `    ``int` `arr[] = { ``1``, ``23``, ``456` `};` `    ``int` `N = arr.length;`   `    ``// Function call` `    ``System.out.println(ConcatenateArr(arr, N));` `}` `}`   `// This code is contributed by avijitmondal1998`

## Python3

 `# Python3 program for the above approach` `import` `math`   `# Function to find the integer value` `# obtained by joining array elements` `# together` `def` `ConcatenateArr(arr,  N):`   `    ``# Stores the resulting integer value` `    ``ans ``=` `arr[``0``]`   `    ``# Traverse the array arr[]` `    ``for` `i ``in` `range``(``1``,  N):`   `        ``# Stores the count of digits of` `        ``# arr[i]` `        ``l ``=` `math.floor(math.log10(arr[i]) ``+` `1``)`   `        ``# Update ans` `        ``ans ``=` `ans ``*` `math.``pow``(``10``, l)`   `        ``# Increment ans by arr[i]` `        ``ans ``+``=` `arr[i]`   `    ``# Return the ans` `    ``return` `int``( ans)`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``# Input` `    ``arr ``=` `[``1``, ``23``, ``456``]` `    ``N ``=` `len``(arr)`   `    ``# Function call` `    ``print``(ConcatenateArr(arr, N))`   `    ``# This code is contributed by ukasp.`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG{`   `// Function to find the integer value` `// obtained by joining array elements` `// together` `static` `int` `ConcatenateArr(``int``[] arr, ``int` `N)` `{` `    `  `    ``// Stores the resulting integer value` `    ``int` `ans = arr[0];`   `    ``// Traverse the array arr[]` `    ``for``(``int` `i = 1; i < N; i++)` `    ``{` `        `  `        ``// Stores the count of digits of` `        ``// arr[i]` `        ``int` `l = (``int``)Math.Floor(Math.Log10(arr[i]) + 1);`   `        ``// Update ans` `        ``ans = ans * (``int``)Math.Pow(10, l);`   `        ``// Increment ans by arr[i]` `        ``ans += arr[i];` `    ``}` `    `  `    ``// Return the ans` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``// Input` `    ``int``[] arr = { 1, 23, 456 };` `    ``int` `N = arr.Length;`   `    ``// Function call` `    ``Console.Write(ConcatenateArr(arr, N));`   `}` `}`   `// This code is contributed by sanjoy_62.`

## Javascript

 ``

Output

```123456

```

Time Complexity: O(N*log(M)), where M is the maximum element of the array.
Auxiliary Space: O(1)

## Method 2 :

Approach : The given problem can be solved using the string functions.

1. Create a resultant string and initialize as an empty string.
2. Simply traverse over each element of the array and convert each of them as string using to_string() function .
3. Now concatenate each string to the resultant string.
4. Then at the end convert the resultant string to integer using the stoi() function.

Refer to the following code for better understanding.

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the integer value` `// obtained by joining array elements` `// together` `int` `ConcatenateArr(``int` `arr[], ``int` `N)` `{` `    ``// Resultant string` `    ``string res = ``""``;`   `    ``// Traverse the array arr[]` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Convert the integer to string` `        ``string curr = to_string(arr[i]);`   `        ``// Concat the curr to res` `        ``res += curr;` `    ``}`   `    ``// Convert the res to integer` `    ``int` `ans = stoi(res);`   `    ``// Return the ans` `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``// Input` `    ``int` `arr[] = { 1, 23, 456 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``// Function call` `    ``cout << ConcatenateArr(arr, N);`   `    ``return` `0;` `}`

## Java

 `public` `class` `ConcatenateArray {` `    ``// Function to find the integer value` `    ``// obtained by joining array elements together` `    ``static` `int` `concatenateArr(``int``[] arr) {` `        ``// Resultant string` `        ``StringBuilder res = ``new` `StringBuilder();`   `        ``// Traverse the array arr[]` `        ``for` `(``int` `num : arr) {` `            ``// Convert the integer to string` `            ``String curr = Integer.toString(num);`   `            ``// Concatenate curr to res` `            ``res.append(curr);` `        ``}`   `        ``// Convert the res to an integer` `        ``int` `ans = Integer.parseInt(res.toString());`   `        ``// Return the ans` `        ``return` `ans;` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``// Input` `        ``int``[] arr = { ``1``, ``23``, ``456` `};`   `        ``// Function call` `        ``System.out.println(concatenateArr(arr));` `    ``}` `}`   `// This code is contributed by shivamgupta0987654321`

## Python

 `# Function to find the integer value obtained by joining array elements together` `def` `concatenate_arr(arr):` `    ``# Initialize an empty string to store the result` `    ``res ``=` `""`   `    ``# Traverse the array arr[]` `    ``for` `num ``in` `arr:` `        ``# Convert the integer to a string` `        ``curr ``=` `str``(num)`   `        ``# Concatenate curr to res` `        ``res ``+``=` `curr`   `    ``# Convert the res to an integer` `    ``ans ``=` `int``(res)`   `    ``# Return the ans` `    ``return` `ans`     `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `    ``# Input` `    ``arr ``=` `[``1``, ``23``, ``456``]`   `    ``# Function call` `    ``print``(concatenate_arr(arr))`

## C#

 `using` `System;`   `class` `Program` `{` `    ``// Function to find the integer value obtained by joining array elements together` `    ``static` `int` `ConcatenateArr(``int``[] arr)` `    ``{` `        ``// Resultant string` `        ``string` `res = ``""``;`   `        ``// Traverse the array arr[]` `        ``foreach` `(``int` `num ``in` `arr)` `        ``{` `            ``// Convert the integer to string` `            ``string` `curr = num.ToString();`   `            ``// Concatenate curr to res` `            ``res += curr;` `        ``}`   `        ``// Convert the res to an integer` `        ``int` `ans = ``int``.Parse(res);`   `        ``// Return the ans` `        ``return` `ans;` `    ``}`   `    ``static` `void` `Main()` `    ``{` `        ``// Input` `        ``int``[] arr = { 1, 23, 456 };`   `        ``// Function call` `        ``Console.WriteLine(ConcatenateArr(arr));` `    ``}` `}`

## Javascript

 `// Function to find the integer value` `// obtained by joining array elements` `// together` `function` `concatenateArray(arr) {` `    ``// Resultant string` `    ``let res = ``""``;`   `    ``// Traverse the array arr[]` `    ``for` `(let i = 0; i < arr.length; i++) {` `        ``// Convert the integer to string` `        ``let curr = arr[i].toString();`   `        ``// Concatenate the curr to res` `        ``res += curr;` `    ``}`   `    ``// Convert the res to integer` `    ``let ans = parseInt(res);`   `    ``// Return the ans` `    ``return` `ans;` `}`   `// Driver Code` `function` `main() {` `    ``// Input` `    ``let arr = [1, 23, 456];`   `    ``// Function call` `    ``console.log(concatenateArray(arr));` `}`   `// Execute the main function` `main();`

Output

```123456

```

Time Complexity : O(N*N), N for traversal and N for using the to_string method
Space Complexity : O(k), where k is the length of the total resultant string