Contents

Add as many elements of an array (with.Java)

   Oct 28, 2023     2 min read

In this article, we learned how to add as many elements as an array.

We’re going to solve a coding test problem, provide a retrospective on the problem we solved, and learn about other ways to solve it.

Let’s start with the problem

Problem

You have an empty array X that doesn’t contain any elements.

Write a solution function that, given an array arr of positive integers as a parameter, iterates over the elements in arr, starting at the front of arr, and if an element is a, adds a to the end of X a number of times, and then returns the array X after iterating over the elements.

Example input and output
arrresult
[5, 1, 4][5, 5, 5, 5, 5, 5, 1, 4, 4, 4, 4]
[6, 6][6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
[1][1]

My solution to the problem

class Solution {
    public int[] solution(int[] arr) {
        int length = 0;
        for(int i = 0; i < arr.length; i++){
            length += arr[i];
        }
        int[] answer = new int[length];
        int idx = 0;
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i]; j++){
                answer[idx++] = arr[i];
            }
        }
    } return answer;
}
Solution

The given code is a function that creates and returns a new array answer by duplicating each element in the integer array arr given as input by the value of that element. Let’s briefly describe the code

int length = 0;: Initialize the variable length to determine the length of the new array answer.

First iteration (for(int i = 0; i < arr.length; i++)): Iterates over the input array arr, adding the value of each element and storing it in the length variable. This determines the total length of the answer array.

int[] answer = new int[length];: Create a new integer array answer with a length based on the length variable.

int idx = 0;: Initialize the variable idx, which keeps track of the index at which to assign a value to the answer array.

The second iteration (for(int j = 0; j < arr[i]; j++) inside for(int i = 0; i < arr.length; i++)): iterates over the array arr again, iterating over each element’s value (arr[i]) and duplicating that element into the array answer.

This ensures that if an element has a value of 2, that element appears twice in the answer array.

return answer;: Returns the finalized answer array.