1.
Max In Array
public class MaxInArray {
public static int findMax(int[] arr) {
// Initialize maximum element with first element of array
int max = arr[0];
// Traverse the array
for (int i = 1; i < [Link]; i++) {
// If the current element is greater than the maximum then update the
maximum
if (arr[i] > max) {
max = arr[i];
}
}
// Return the maximum element
return max;
}
public static void main(String[] args) {
// Declare and initialize the array
int[] arr = { 25, 11, 7, 75, 56 };
// Call the function to find the maximum element
int MaxInArray = findMax(arr);
[Link]("23MCA1030 Vinayak Kumar Singh");
// Print the maximum element
[Link]("The maximum element in the array is: " +
MaxInArray);
}
}
2. Java program to reverse an array without using any additional array.
public class ArrayReverseNoExtraSpace {
// Function to reverse the array in-place
public static void reverseArray(int[] arr) {
int start = 0;
int end = [Link] - 1;
// Swap elements at the start and end indices
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original array: [");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]);
if (i < [Link] - 1) {
[Link](", ");
}
}
[Link]("]");
reverseArray(arr);
[Link]("Reversed array: [");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]);
if (i < [Link] - 1) {
[Link](", ");
}
}
[Link]("]");
}}
3. Java program to sort an array of integers in ascending order using the
bubble sort algorithm.
public class BubbleSort {
// Function to sort the array using bubble sort algorithm
public static void bubbleSort(int[] arr) {
int n = [Link];
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no two elements were swapped by the inner loop, break
if (!swapped) {
break;
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original array: [");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]);
if (i < [Link] - 1) {
[Link](", ");
}
}
[Link]("]");
bubbleSort(arr);
[Link]("Sorted array: [");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]);
if (i < [Link] - 1) {
[Link](", ");
}
}
[Link]("]");
}
}
4. Java program to find the second largest element in an array
public class SecondLargestInArray {
public static void main(String[] args) {
int[] arr = {5, 8, 2, 10, 7, 3};
int secondLargest = findSecondLargest(arr);
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original array is: ");
printArray(arr);
[Link]("\nThe second largest element in array is: " +
secondLargest);
}
public static int findSecondLargest(int[] arr) {
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
// Find the largest element
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
}
}
// Update the second largest element
for (int num : arr) {
if (num > secondLargest && num != largest) {
secondLargest = num;
}
}
return secondLargest;
}
public static void printArray(int[] arr) {
for (int i = 0; i < [Link]; i++) {
if (i > 0) {
[Link](", ");
}
[Link](arr[i]);
}
}
}
5. Java program to remove duplicate elements from an array
without using any additional data structure.
public class RemoveDuplicatesInArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2, 5, 3, 6};
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original array: ");
printArray(arr);
int length = removeDuplicates(arr);
[Link]("\nArray after removing duplicates: ");
printArray(arr, length);
}
public static int removeDuplicates(int[] arr) {
if ([Link] == 0) {
return 0;
}
int j = 0;
for (int i = 1; i < [Link]; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return j + 1;
}
public static void printArray(int[] arr) {
for (int num : arr) {
[Link](num + " ");
}
}
public static void printArray(int[] arr, int length) {
for (int i = 0; i < length; i++) {
[Link](arr[i] + " ");
}
}
}
6. Java program to compute the sum of elements in a 2D array.
public class SumOf2DArray {
public static int sumOf2DArray(int[][] arr) {
int sum = 0;
for (int[] row : arr) {
for (int num : row) {
sum += num;
}
}
return sum;
}
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = sumOf2DArray(arr);
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Sum of elements in the 2D array: " + sum);
}
}
7. Java program to find the intersection of two arrays.
import [Link];
import [Link];
public class ArrayIntersection {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {4, 5, 6, 7, 8};
int[] intersection = findIntersection(arr1, arr2);
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Intersection of the two arrays: ");
printArray(intersection);
}
public static int[] findIntersection(int[] arr1, int[] arr2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> intersection = new HashSet<>();
// Add all elements of arr1 to set1
for (int num : arr1) {
[Link](num);
}
// Get intersection by checking elements of arr2 in set1
for (int num : arr2) {
if ([Link](num)) {
[Link](num);
}
}
// Convert the intersection set to an array
int[] intersectionArray = new int[[Link]()];
int index = 0;
for (int num : intersection) {
intersectionArray[index++] = num;
}
return intersectionArray;
}
public static void printArray(int[] arr) {
for (int num : arr) {
[Link](num + " ");
}
[Link]();
}
}
8. Java program to rotate an array to the right by a given number of
steps.
public class ArrayRotate {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int steps = 2;
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original array: ");
printArray(arr);
rotateArray(arr, steps);
[Link]("\nArray after rotation: ");
printArray(arr);
}
public static void rotateArray(int[] arr, int steps) {
int n = [Link];
steps = steps % n; // Normalize steps to work for any value
// Reverse entire array
reverseArray(arr, 0, n - 1);
// Reverse first n - steps elements
reverseArray(arr, 0, n - steps - 1);
// Reverse remaining steps elements
reverseArray(arr, n - steps, n - 1);
}
public static void reverseArray(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void printArray(int[] arr) {
for (int num : arr) {
[Link](num + " ");
}
[Link]();
}
}
9. Java program to find the frequency of each element in an array.
import [Link];
import [Link];
public class FindFrequency {
public static Map<Integer, Integer> findFrequency(int[] arr) {
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : arr) {
[Link](num, [Link](num, 0) + 1);
}
return frequencyMap;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 1, 5, 2, 3};
Map<Integer, Integer> frequencyMap = findFrequency(arr);
[Link]((key, value) -> [Link](key + " -> " +
value));
}
}
10. Java program to check if two arrays are equal or not.
public class ArrayEqual {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 2, 3, 4, 5};
int[] arr3 = {5, 4, 3, 2, 1};
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("arr1 and arr2 equal? " + areArraysEqual(arr1, arr2));
[Link]("arr1 and arr3 equal? " + areArraysEqual(arr1, arr3));
}
public static boolean areArraysEqual(int[] arr1, int[] arr2) {
// Check if the arrays have the same length
if ([Link] != [Link]) {
return false;
}
// Compare each element of the arrays
for (int i = 0; i < [Link]; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
}
[Link] program to add elements to an ArrayList and display its contents
import [Link];
public class ArrayListAddQuestion{
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> names = new ArrayList<>();
// Add elements to the ArrayList
[Link]("Aman");
[Link]("Binay");
[Link]("Chirag");
[Link]("Dheeraj");
// Display contents of the ArrayList
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Contents of the ArrayList:");
for (String name : names) {
[Link](name);
}
}
}
12. Java program to remove a specific element from an ArrayList.
import [Link];
public class RemoveElementArrayList {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> names = new ArrayList<>();
[Link]("Aman");
[Link]("Binay");
[Link]("Chirag");
[Link]("Dheeraj");
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original ArrayList: " + names);
// Remove a specific element
String elementToRemove = "Chirag";
boolean isRemoved = [Link](elementToRemove);
if (isRemoved) {
[Link]("Element \"" + elementToRemove + "\" is removed
from the ArrayList.");
} else {
[Link]("Element \"" + elementToRemove + "\" not found
in the ArrayList.");
}
[Link]("Updated ArrayList: " + names);
}
}
13. Java program to sort an ArrayList of strings in alphabetical
import [Link];
import [Link];
public class SortArrayList {
public static void main(String[] args) {
// Create an ArrayList of strings
ArrayList<String> names = new ArrayList<>();
[Link]("Vinayak");
[Link]("Aman");
[Link]("Raj");
[Link]("Binay");
[Link]("Chirag");
[Link]("Tanmay");
[Link]("Dheeraj");
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original ArrayList: " + names);
// Sort the ArrayList in alphabetical order
[Link](names);
[Link]("Sorted ArrayList: " + names);
}
}
14. Java program to remove all duplicate elements from an ArrayList
import [Link];
import [Link];
public class RemoveDuplicateArrayList {
public static void main(String[] args) {
// Create an ArrayList with duplicates
ArrayList<String> names = new ArrayList<>();
[Link]("Aman");
[Link]("Aman");
[Link]("Raj");
[Link]("Binay");
[Link]("Chirag");
[Link]("Chirag");
[Link]("Tanmay");
[Link]("Dheeraj");
[Link]("Original ArrayList: " + names);
// Remove duplicates using a LinkedHashSet
LinkedHashSet<String> uniqueNames = new LinkedHashSet<>(names);
ArrayList<String> namesWithoutDuplicates = new
ArrayList<>(uniqueNames);
[Link]("ArrayList without duplicates: " +
namesWithoutDuplicates);
}
}
15. Java program to find the length of the longest subsequence
of increasing integers in an ArrayList.
import [Link];
public class LongestIncreasingSubsequence {
public static int findLongestSubsequenceLength(ArrayList<Integer> nums)
{
int maxLength = 1; // Length of longest subsequence found so far
int currentLength = 1; // Length of current subsequence being considered
for (int i = 1; i < [Link](); i++) {
if ([Link](i) > [Link](i - 1)) {
currentLength++;
maxLength = [Link](maxLength, currentLength);
} else {
currentLength = 1; // Reset for new subsequence
}
}
return maxLength;
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>() {{
add(4);
add(2);
add(3);
add(6);
add(10);
add(1);
add(12);
}};
int maxLength = findLongestSubsequenceLength(list);
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Length of longest increasing subsequence: " +
maxLength);
}
}
16. Java program to shuffle the elements of an ArrayList.
import [Link];
import [Link];
public class ShuffleArrayList {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>() {{
add("Blue");
add("Yellow");
add("Purple");
add("Red");
add("Green");
}};
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Original list: " + list);
// Using [Link]() method:
[Link](list);
[Link]("Shuffled list ([Link]()): " + list);
}
}
17. Java program to find the intersection of two ArrayLists.
import [Link];
import [Link];
import [Link];
public class IntersectionArrayList {
public static ArrayList<Integer> findIntersection(ArrayList<Integer> list1,
ArrayList<Integer> list2) {
Set<Integer> set1 = new HashSet<>(list1);
Set<Integer> intersection = new HashSet<>();
for (int num : list2) {
if ([Link](num)) {
[Link](num);
}
}
return new ArrayList<>(intersection);
}
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>() {{
add(1);
add(2);
add(3);
add(4);
add(5);
}};
ArrayList<Integer> list2 = new ArrayList<>() {{
add(3);
add(4);
add(5);
add(6);
add(7);
}};
ArrayList<Integer> intersection = findIntersection(list1, list2);
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Intersection: " + intersection);
}
}
18. Java program to convert an ArrayList to an array.
import [Link];
import [Link];
public class ArrayListToArray {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> fruits = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Date");
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("ArrayList: " + fruits);
// Convert ArrayList to an array
String[] fruitsArray = [Link](new String[0]);
[Link]("Array: " + [Link](fruitsArray));
}
}
19. Java program to find the union of two ArrayLists.
import [Link];
import [Link];
import [Link];
import [Link];
public class UnionArrayList {
public static List<Integer> findUnion(ArrayList<Integer> list1,
ArrayList<Integer> list2) {
Set<Integer> set = new HashSet<>();
[Link](list1);
[Link](list2);
return new ArrayList<>(set);
}
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>() {{
add(1);
add(2);
add(3);
add(4);
add(5);
}};
ArrayList<Integer> list2 = new ArrayList<>() {{
add(3);
add(4);
add(5);
add(6);
add(7);
}};
List<Integer> union = findUnion(list1, list2);
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("Union: " + union);
}
}
[Link] program to check if an ArrayList is empty or not.
import [Link];
public class ArrayListEmptyChecker {
public static void main(String[] args) {
// Create an empty ArrayList
ArrayList<String> myList1 = new ArrayList<>();
// Create a non-empty ArrayList
ArrayList<Integer> myList2 = new ArrayList<Integer>() {{
add(1);
add(2);
add(3);
}};
// Check if the ArrayLists are empty
[Link]("23MCA1030 Vinayak Kumar Singh");
[Link]("myList1 is empty: " + [Link]());
[Link]("myList2 is empty: " + [Link]());
}
}