Skip to main content

Arrays

Arrays: Practice Problems I

  1. Write a java program to find second largest element in an array. If not exist, then return -1.

Input

Enter the size of the array: 
10
Enter array elements:
7 2 3 8 6 6 75 38 3 2

Output

38
Solution Code
import java.util.Scanner;

public class SecondLargestElement {

    public static void main(String[] args) {
        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);
  
        // Ask the user for the size of the array
        System.out.print("Enter the size of the array: ");
        int n = scanner.nextInt();
  
        // Create an array to store user-input elements
        int array[] = new int[n];
  
        // Prompt the user to enter array elements
        System.out.println("Enter array elements:");
  
        // Loop to get array input elements
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }

        int largest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;

        for (int i = 0; i < array.length; i++) {
            int num = array[i];

            if (num > largest) {
                secondLargest = largest;
                largest = num;
            } else if (num > secondLargest && num != largest) {
                secondLargest = num;
            }
        }

        if (secondLargest == Integer.MIN_VALUE) {
            System.out.println("-1");
        } else {
            System.out.println(secondLargest);
        }
    }
}
  1. Write a java program to input elements in array from user and count duplicate elements in array and print them.

Input

10
1 2 3 4 2 7 8 8 3 5

Output

2
3
8
Count of duplicates - 3
Hint
To find and print duplicate elements in an array, a common approach involves using two loops. 

The first loop selects an element, and the second loop iterates through the array, comparing the selected element with others. 

If a match is found, the duplicate element is printed and increase the count.
Solution Code
import java.util.Scanner;

class Main {
 
   public static void main(String[] args) {

      // Create a Scanner object for user input
      Scanner scanner = new Scanner(System.in);

      // Ask the user for the size of the array
      System.out.print("Enter the size of the array: ");
      int n = scanner.nextInt();

      // Create an array to store user-input elements
      int num[] = new int[n];

      // Prompt the user to enter array elements
      System.out.println("Enter array elements:");

      // Loop to get input elements
      for (int i = 0; i < n; i++) {
          num[i] = scanner.nextInt();
      }
                                
      int count = 0;
	  //Searches for duplicate element  
      for(int i = 0; i < num.length; i++) {  
        for(int j = i + 1; j < num.length; j++) {  
            if(num[i] == num[j])  {
              System.out.println(num[j]);  
			  count++;
		    }	  
          }  
      }  
   
      System.out.println("Count of duplicates - " + count);
 
  }
 
}
  1. Write a Java program to left rotate an array by k position.

Input

Enter the size of the array: 
10
Enter array elements:
1 2 3 4 5 6 7 8 9 10
Enter k:
3

Output

4 5 6 7 8 9 10 1 2 3
Hint
To solve the problem of rotating an array, you can create a temporary array to store the rotated version. 

Use a loop to copy the rotated elements from the original array to the temporary array. Pay attention to index manipulation to ensure the elements are copied correctly. 

Finally, copy the elements from the temporary array back to the original array to achieve the desired rotation. 
Solution Code
import java.util.Scanner;

class Main {
 
   public static void main(String[] args) {

		// Create a Scanner object for user input
		Scanner scanner = new Scanner(System.in);

		// Ask the user for the size of the array
		System.out.print("Enter the size of the array: ");
		int n = scanner.nextInt();

		// Create an array to store user-input elements
		int num[] = new int[n];

		// Prompt the user to enter array elements
		System.out.println("Enter array elements:");

		// Loop to get input elements and calculate the sum
		for (int i = 0; i < n; i++) {
			num[i] = scanner.nextInt();
		}

        System.out.println("Enter k:");							
		int rotateBy = scanner.nextInt();
		// Create a temporary array to store the rotated version
		int temp[] = new int[n];
	
		// Keep track of the current index in temp[]
		int tempIndex = 0;
	
		// Store the rotated elements (from index rotateBy to the end) in temp[]
		for (int i = rotateBy; i < n; i++) {
			temp[tempIndex] = num[i];
			tempIndex++;
		}
	
		// Store the first rotateBy elements of array num[] in temp
		for (int i = 0; i < rotateBy; i++) {
			temp[tempIndex] = num[i];
			tempIndex++;
		}
	
		// Copy the elements from temp[] back to num[] to get the final rotated array
		for (int i = 0; i < n; i++) {
			num[i] = temp[i];
		}

		for (int i = 0; i < n; i++) {
          System.out.println(num[i]);
        }
	
  }
 
}