Top 5 Array Program Which Is Generally Asked In Interviews

                   Top 5 Array Program


1.) find the second largest number in an array of integers?


import java.util.Scanner;

public class SecondLargest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print
("Enter the size of the array: ");
int size = scanner.nextInt();
int[] arr = new int[size];
System.out.println
("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
arr[i] = scanner.nextInt();
        }
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
if (arr[i] > max) {
secondMax = max;
max = arr[i];
} else if (arr[i] >
secondMax && arr[i] != max) {
                secondMax = arr[i];
            }
        }
if (secondMax == Integer.MIN_VALUE) {
System.out.println
("There is no second largest element");
        } else {
System.out.println
("The second largest element is: "
+ secondMax);
        }
        scanner.close();
    }
}


2.) How do you reverse an array in-place?


import java.util.Arrays;

public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println
("Original array:"
+Arrays.toString(arr));
        reverseArray(arr);
 System.out.println
("Reversed array: " +
Arrays.toString(arr));
    }
   
public static void
reverseArray(int[] arr) {
        int i = 0;
        int j = arr.length - 1;
        while (i < j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }
}


3.) How do you remove duplicates from an array?


import java.util.Arrays;

public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4, 4, 5};
System.out.println
("Original array: "
+ Arrays.toString(arr));
int[] uniqueArr = removeDuplicates(arr);
System.out.println
("Array with duplicates removed: "
+ Arrays.toString(uniqueArr));
    }
   
public static int[]
removeDuplicates(int[] arr) {
        int n = arr.length;
        int j = 0;
        for (int i = 0; i < n - 1; i++) {
            if (arr[i] != arr[i+1]) {
                arr[j++] = arr[i];
            }
        }
        arr[j++] = arr[n-1];
        int[] uniqueArr = new int[j];
        for (int i = 0; i < j; i++) {
            uniqueArr[i] = arr[i];
        }
        return uniqueArr;
    }
}


4.) How do you find the sum of all elements in an array?


import java.util.Arrays;

public class ArraySum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Array: " +
Arrays.toString(arr));
int sum = getArraySum(arr);
System.out.println
("Sum of all elements: " + sum);
    }
   
public static int getArraySum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
        }
        return sum;
    }
}


5.) How do you check if an array contains a specific element?


import java.util.Arrays;

public class ArrayContains {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Array: "
+ Arrays.toString(arr));
int x = 3;
boolean containsX =
arrayContains(arr, x);
if (containsX) {
System.out.println
("Array contains " + x);
        } else {
System.out.println
("Array does not contain " + x);
        }
    }
   
public static boolean
arrayContains(int[] arr, int x) {
for (int i = 0; i < arr.length; i++) {
            if (arr[i] == x) {
                return true;
            }
        }
        return false;
    }
}



In conclusion, we hope you enjoyed reading our post and found it informative and valuable. We put a lot of effort into creating high-quality content and would love to hear your thoughts and feedback. So, please do leave a comment and let us know what you think. Additionally, we invite you to visit our website www.javaoneworld.com to read more beautifully written posts on various topics related to coding, programming, and technology. We are constantly updating our website with fresh and valuable content that will help you improve your skills and knowledge. We are excited to have you as a part of our community, and we look forward to connecting with you and providing you with more informative and valuable content in the future. 

Happy coding!✌✌

No comments:

Post a Comment