Category: Java programming question
-
Merge Sorted Array
Problem Statement You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements…
-
Java program to calculate the age of a person in years given their birthday
You can calculate a person’s age in years using Java 8’s LocalDate and Period API. Using ChronoUnit.YEARS
-
Java program to find the first non-repeated character in a string
You can find the first non-repeated character in a string using Java 8 Streams by counting occurrences and filtering the first unique character. Output: Alternative: Using Traditional Map Approach (Better Performance)
-
Java Program to find the first repeated character in a String
Output: Alternative: Using Traditional Set Approach (Better Performance)
-
Java program to find duplicate characters in a String
Output: Alternative: Using Set for Better Performance
-
Java program to find duplicate elements in an array
You can extract duplicate elements from an array using Java 8 Streams by grouping elements and filtering those that appear more than once. Output: Alternative: Using Set for Better Performance If the array is large, using a Set avoids additional memory usage:
-
Java Program to find the most repeated element in an array
Output: Explanation: Alternative: Handling Multiple Max Elements If multiple elements have the same highest frequency, return all: Output:
-
Java Program reverse each word in a String
You can reverse each word in a string using Java 8 Streams by splitting the string into words, reversing each word, and then joining them back. Output: