Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Sunday 18 June 2023

Find the Kth Maximum and Minimum element from Array?


2 / 2

This is one of the most asked question from array in interviews. Find the "Kth" maximum and minimum element of an array in Java. This question is asked to check the logical skill and java knowledge of candidate. you can follow below steps to solve this problem:

  1. Sort the array in ascending order.
  2. The "Kth" minimum element will be at index K-1.
  3. The "Kth" maximum element will be at index array.length - K.

Here's an example code snippet that demonstrates this approach:

java
import java.util.Arrays;
public class KthMinMaxElement
public static void main(String[] args)
int[] array = {9, 5, 2, 7, 1, 8, 6, 3, 4};
// Sort the array in ascending order 
 Arrays.sort(array);
int k = 3;
// The value of k // Find the Kth minimum element
int kthMin = array[k - 1];
 System.out.println("Kth minimum element: " + kthMin);
// Find the Kth maximum element
int kthMax = array[array.length - k];
 System.out.println("Kth maximum element: " + kthMax);
 }
 }

In this example, the array is {9, 5, 2, 7, 1, 8, 6, 3, 4}. We want to find the 3rd minimum and maximum elements. The sorted array is {1, 2, 3, 4, 5, 6, 7, 8, 9}, so the 3rd minimum element is 3, and the 3rd maximum element is 7.

You can modify the code according to your specific requirements by providing a different array and value for k. 

Thursday 15 June 2023

How to find the duplicate elements in java array ?

To find duplicate elements in an array in Java, you can use various approaches. Here are a few commonly used methods:

  1. Using Brute Force: You can use a nested loop to compare each element of the array with all other elements and check for duplicates. If a duplicate is found, you can store or print the element.

    java
    int[] array = {1, 2, 3, 4, 2, 5, 1}; for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] == array[j]) { System.out.println("Duplicate element: " + array[i]); } } }
  2. Using HashSet: You can utilize a HashSet to keep track of unique elements encountered in the array. When adding elements to the HashSet, if an element is already present, it indicates a duplicate.

    java
    int[] array = {1, 2, 3, 4, 2, 5, 1}; Set<Integer> uniqueElements = new HashSet<>(); Set<Integer> duplicateElements = new HashSet<>(); for (int num : array) { if (!uniqueElements.add(num)) { duplicateElements.add(num); } } System.out.println("Duplicate elements: " + duplicateElements);
  3. Using HashMap: You can utilize a HashMap to store the frequency of each element in the array. If the frequency exceeds 1, it indicates a duplicate element.

    java
    int[] array = {1, 2, 3, 4, 2, 5, 1}; Map<Integer, Integer> elementFrequency = new HashMap<>(); for (int num : array) { elementFrequency.put(num, elementFrequency.getOrDefault(num, 0) + 1); } for (Map.Entry<Integer, Integer> entry : elementFrequency.entrySet()) { if (entry.getValue() > 1) { System.out.println("Duplicate element: " + entry.getKey()); } }

These are a few approaches to find duplicate elements in an array in Java. Choose the one that suits your requirements and the size of the array.