Skip to content

Commit 690042c

Browse files
committed
feat: Implement deletion from array by shifting elements left
🧠 Logic: - Deletes an element at a specific index by shifting all subsequent elements one position to the left. - Reduces the logical size of the array to reflect the deletion. - Demonstrates a basic array deletion algorithm without using built-in methods. Signed-off-by: Somesh diwan <[email protected]>
1 parent 2f62166 commit 690042c

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed
Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
/*
2-
Merging 2 arrays
3-
Merging two 1D arrays involves combining the elements of both arrays into a single array while maintaining their original order.
2+
Merging 2 arrays:
3+
Merging two 1D arrays involves combining the elements of both arrays into a single array while maintaining
4+
their original order.
45
56
Here's a step-by-step process for merging two 1D arrays:
67
7-
Create a New Array: Create a new array that is large enough to hold the combined elements of both arrays. The size of the new array should be the sum of the sizes of the two original arrays.
8-
Copy Elements: Iterate through the elements of the first array and copy them to the new array. Then, iterate through the elements of the second array and copy them to the new array after the elements of the first array.
8+
Create a New Array: Create a new array that is large enough to hold the combined elements of both arrays.
9+
The size of the new array should be the sum of the sizes of the two original arrays.
10+
Copy Elements: Iterate through the elements of the first array and copy them to the new array.
11+
12+
Then, iterate through the elements of the second array and copy them to the new array after the elements of the first array.
913
Result: The new array now contains all the elements from both original arrays, merged in the desired order.
10-
Task
14+
1115
You are given a template code in the IDE.
12-
Update the code to merge the 2 arrays based on the process defined above.*/
16+
Update the code to merge the 2 arrays based on the process defined above.
17+
*/
1318

1419
public class ArrayIMPCC2{
1520
public static void main(String[] args) {
@@ -26,51 +31,53 @@ public static void main(String[] args) {
2631
secondArray[1] = 10;
2732
secondArray[2] = 12;
2833
secondArray[3] = 14;
29-
int size2 = 4;
3034

31-
// Update the code below to solve the problem.
35+
int size2 = 4;
3236

3337
int mergedSize = size1 + size2;
3438

3539
int mergedArray[] = new int[mergedSize];
3640

37-
for(int i = 0; i < size1; i++)
38-
{
41+
for(int i = 0; i < size1; i++) {
3942
mergedArray[i] = firstArray[i];
4043
}
4144

42-
for(int i = 0; i<size2; i++)
43-
{
45+
for(int i = 0; i<size2; i++) {
4446
mergedArray[size1+i] = secondArray[i];
4547
}
4648

47-
for(int i = 0; i < mergedSize; i++)
48-
{
49+
for(int i = 0; i < mergedSize; i++) {
4950
System.out.print(mergedArray[i] + " ");
5051
}
5152
}
5253
}
5354

5455
/*
55-
If you directly print the merged array without using a for loop, Java will print the array's memory address (hash code) instead of its actual elements. This happens because arrays in Java are objects, and the default toString() method for arrays doesn't override the Object class's toString() method.
56+
If you directly print the merged array without using a for loop,
57+
Java will print the array's memory address (hash code) instead of its actual elements.
58+
59+
This happens because arrays in Java are objects, and the default toString() method for arrays doesn't
60+
override the Object class's toString() method.
5661
57-
For example:
62+
For example:
5863
59-
System.out.println(mergedArray);
60-
This will output something like: [I@1b6d3586
64+
System.out.println(mergedArray);
65+
This will output something like: [I@1b6d3586
6166
62-
This is not useful for understanding the contents of the array. To print the actual elements of the array, you must use a loop or a method like Arrays.toString().
67+
This is not useful for understanding the contents of the array.
68+
To print the actual elements of the array, you must use a loop or a method like Arrays.toString().
6369
64-
Correct Way to Print Without a Loop
65-
If you want to avoid writing a for loop, you can use Arrays.toString() from the java.util.Arrays class:
70+
Correct Way to Print Without a Loop
71+
If you want to avoid writing a for loop, you can use Arrays.toString() from the java.util.Arrays class:
6672
67-
import java.util.Arrays;
73+
import java.util.Arrays;
74+
System.out.println(Arrays.toString(mergedArray));
75+
This will print the array in a readable format,
6876
69-
System.out.println(Arrays.toString(mergedArray));
70-
This will print the array in a readable format, like:
77+
like:
78+
[2, 4, 6, 8, 10, 12, 14]
7179
72-
[2, 4, 6, 8, 10, 12, 14]
73-
Key Takeaway
74-
Directly printing an array will not show its elements.
75-
Use Arrays.toString() for a quick and clean way to print the array without writing a loop.
80+
Key Takeaway
81+
Directly printing an array will not show its elements.
82+
Use Arrays.toString() for a quick and clean way to print the array without writing a loop.
7683
*/

0 commit comments

Comments
 (0)