0% found this document useful (0 votes)
2 views3 pages

Array Logical Code

The document outlines various array manipulation programs in Java, including finding the largest and second largest elements, reversing an array, removing duplicates, and merging two arrays. It provides sample code for each operation, demonstrating how to implement these functionalities using loops, sets, and hash maps. Additionally, it covers tasks like finding missing numbers, counting element frequencies, and moving zeros to the end of an array.

Uploaded by

sreshu507
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Array Logical Code

The document outlines various array manipulation programs in Java, including finding the largest and second largest elements, reversing an array, removing duplicates, and merging two arrays. It provides sample code for each operation, demonstrating how to implement these functionalities using loops, sets, and hash maps. Additionally, it covers tasks like finding missing numbers, counting element frequencies, and moving zeros to the end of an array.

Uploaded by

sreshu507
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ARRAY PROGRAMS if(num > first) {

second = first;
first = num;
1️⃣ Find largest element } else if(num > second && num !=
2️⃣ Find second largest element first) {
3️⃣ Reverse an array second = num;
4️⃣ Remove duplicates from array }
5️⃣ Find missing number in array }
6️⃣ Find duplicate elements [Link]("Second Largest
7️⃣ Sort array without using sort() Element: " + second);
8️⃣ Merge two arrays }
9️⃣ Frequency of each element }
(HashMap)
3. Reverse an Array
🔟 Move all zeros to end of array
 Take an integer array as input.
1. Find Largest Element in Array  Use two pointers: start and end.
 Take an integer array as input.  Swap elements at start and end.
 Initialize a variable max with the first  Move pointers inward until they meet.
element.  Print the reversed array.
 Loop through the array. Sample Code:
 Compare each element with max. java
 If greater, update max. public class ReverseArray {
 Print the largest element. public static void main(String[] args) {
Sample Code: int[] arr = {1, 2, 3, 4, 5};
java int start = 0, end = [Link] - 1;
public class LargestElement {
public static void main(String[] args) { while(start < end) {
int[] arr = {10, 25, 5, 40, 30}; int temp = arr[start];
int max = arr[0]; arr[start] = arr[end];
arr[end] = temp;
for(int i = 1; i < [Link]; i++) { start++;
if(arr[i] > max) { end--;
max = arr[i]; }
}
} [Link]("Reversed Array: ");
[Link]("Largest Element: " + for(int num : arr) {
max); [Link](num + " ");
} }
} }
2. Find Second Largest Element }
 Take an integer array as input. 4. Remove Duplicates from Array
 Initialize two variables first and  Take an integer array as input.
second.  Use a LinkedHashSet to store unique
 Loop through the array. elements.
 Update first and second accordingly.  Print the set as the result.
 Print the second largest element. Sample Code:
Sample Code: java
java import [Link].*;
public class SecondLargest {
public static void main(String[] args) { public class RemoveDuplicates {
int[] arr = {10, 25, 5, 40, 30}; public static void main(String[] args) {
int first = Integer.MIN_VALUE, second int[] arr = {1, 2, 2, 3, 4, 4, 5};
= Integer.MIN_VALUE; Set<Integer> set = new
LinkedHashSet<>();
for(int num : arr) {
for(int num : arr) { }
[Link](num); }
} }
7. Sort Array Without Using sort()
[Link]("Array without  Take an integer array as input.
duplicates: " + set);  Use nested loops.
}  Compare each element with others.
}  Swap if out of order.
5. Find Missing Number in Array  Print sorted array.
 Take an array of size n-1 with Sample Code:
numbers from 1 to n. java
 Calculate sum of first n natural public class SortArray {
numbers. public static void main(String[] args) {
 Calculate sum of array elements. int[] arr = {5, 2, 8, 1, 3};
 Subtract array sum from natural sum
to get missing number. for(int i = 0; i < [Link]; i++) {
Sample Code: for(int j = i + 1; j < [Link]; j++) {
java if(arr[i] > arr[j]) {
public class MissingNumber { int temp = arr[i];
public static void main(String[] args) { arr[i] = arr[j];
int[] arr = {1, 2, 4, 5}; arr[j] = temp;
int n = 5; }
int totalSum = n * (n + 1) / 2; }
int arrSum = 0; }

for(int num : arr) { [Link]("Sorted Array: ");


arrSum += num; for(int num : arr) {
} [Link](num + " ");
}
int missing = totalSum - arrSum; }
[Link]("Missing Number: " + }
missing); 8. Merge Two Arrays
}  Take two arrays as input.
}  Create a new array of combined
length.
6. Find Duplicate Elements  Copy elements of first array.
 Take an integer array as input.  Copy elements of second array.
 Use a HashSet to track seen elements.  Print merged array.
 If element already exists, print as Sample Code:
duplicate. java
Sample Code: public class MergeArrays {
java public static void main(String[] args) {
import [Link].*; int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
public class FindDuplicates { int[] merged = new int[[Link] +
public static void main(String[] args) { [Link]];
int[] arr = {1, 2, 3, 2, 4, 5, 1};
Set<Integer> set = new HashSet<>(); int index = 0;
for(int num : arr1) {
[Link]("Duplicate Elements: "); merged[index++] = num;
for(int num : arr) { }
if(![Link](num)) { for(int num : arr2) {
[Link](num + " "); merged[index++] = num;
} }
[Link](num, [Link](num,
[Link]("Merged Array: "); 0) + 1);
for(int num : merged) { }
[Link](num + " ");
} [Link]("Frequency of
} Elements: " + map);
} }
9. Frequency of Each Element (HashMap) }
 Take an integer array as input. 10. Move All Zeros to End of Array
 Use a HashMap to store element  Take an integer array as input.
counts.  Use a pointer index to track non-zero
 Loop through array and update elements.
frequency.  Traverse array and move non-zero
 Print the map. elements forward.
Sample Code:  Fill remaining positions with zeros.
java  Print the result.
import [Link].*; Sample Code:
java
public class FrequencyCount { public class MoveZeros {
public static void main(String[] args) { public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 3, 3, 4}; int[] arr = {0, 1, 0, 3, 12};
Map<Integer, Integer> map = new int index = 0;
HashMap<>();
for(int i = 0; i < [Link]; i++) {
for(int num : arr) { if(arr[i] != 0) {
arr[index++] = arr[i];
}
}

while(index < [Link]) {


arr[index++] = 0;
}

[Link]("Array after moving zeros: ");


for(int num : arr) {
[Link](num + " ");
}
}
}

You might also like