Skip to content

Commit e93f35c

Browse files
committed
Feat/Adds a getConcatenation method that doubles an arrays contents
1 parent 7b3bd25 commit e93f35c

File tree

1 file changed

+35
-2
lines changed
  • lesson_11/arrays_java/arrays_app/src/main/java/com/codedifferently/lesson11

1 file changed

+35
-2
lines changed

lesson_11/arrays_java/arrays_app/src/main/java/com/codedifferently/lesson11/Lesson11.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,41 @@ public class Lesson11 {
88
* Provide the solution to LeetCode 1929 here:
99
* https://leetcode.com/problems/concatenation-of-array
1010
*/
11-
public int[] getConcatenation(int[] nums) {
12-
return null;
11+
public static int[] getConcatenation(int[] nums) {
12+
13+
int n = nums.length; //Creates a variable named n that is the length of nums
14+
int[] ans = new int[2 * n]; //Creates a new array that is double the size of n.
15+
16+
17+
for (int i = 0; i < n; ++i) {
18+
ans[i] = nums[i]; //Gets the first half of the int array
19+
ans[i + n] = nums[i]; //Gets the second half of the int array
20+
}
21+
return ans;
22+
}
23+
24+
25+
public static void main(String[] args) {
26+
27+
//Creates an instance of the solution class
28+
//We need this because getConcatenation() is a method that is non-static meaning we need and object of Solution to use it.
29+
//This would be unneeded if getConcatenation() was static
30+
31+
32+
int[] nums = {1, 2, 3, 4}; //Array of nums based on Test input
33+
int[] result = getConcatenation(nums); //calls the getConcatenation method giving it the array of nums.
34+
35+
36+
//Prints result out.
37+
38+
//You need to loop out the result because we want to display the contents of an array which means we need to call each instance in it. This is easily done by using the for else loop to run until it is returned false.
39+
for (int num: result) {
40+
System.out.print(num + " ");
41+
}
42+
}
43+
}
44+
45+
1346
}
1447

1548
/**

0 commit comments

Comments
 (0)