Code Snippets
Runnable Java code examples for testers: arrays, numbers and strings, with output and common mistakes.
Copy One Array to Another in Java (Shallow vs Deep Copy Explained)
Learn every way to copy an array in Java — clone(), Arrays.copyOf(), System.arraycopy() — and the crucial shallow vs deep copy distinction.
Merge Two Arrays in Java (With and Without Sorting)
Learn how to merge two arrays in Java using System.arraycopy(), manual loops, and streams, including sorted-merge techniques.
Compare Two Arrays for Equality in Java (Arrays.equals() & Beyond)
Learn how to check if two arrays are equal in Java using Arrays.equals(), deepEquals(), and manual loops, including order-sensitive cases.
Find Sum of Even and Odd Elements Separately in a Java Array
Learn to calculate the sum of even and odd elements separately in a Java array using loops and streams, with full code examples.
Count Positive and Negative Numbers in a Java Array (Complete Guide)
Learn how to count positive, negative, and zero values in a Java array using loops and streams, including the zero classification rule.
Separate Even and Odd Elements into Two Arrays in Java
Learn how to split a Java array into separate even and odd element arrays using two-pass counting and dynamic ArrayList methods.
Count Even and Odd Elements in a Java Array
Learn how to count even and odd elements in a Java array using the modulo operator, with negative number handling and streams.
Find the First Duplicate Element in a Java Array
Learn how to find the first duplicate element in a Java array in a single pass with HashSet, including the "first by index" nuance
Find Unique Elements in an Array in Java
Learn how to find elements that appear exactly once in a Java array using HashMap, LinkedHashSet, and the XOR trick for special cases
Count Frequency of Each Element in an Array in Java (HashMap Guide)
Learn how to count the frequency of each element in a Java array using HashMap, streams, and sorting-based approaches, with examples.
Remove Duplicates from an Array in Java
Learn how to remove duplicate elements from a Java array using LinkedHashSet, two pointers, and streams while preserving order.
Reverse an Array in Java (In-Place, Recursive & Stream Methods)
Learn how to reverse an array in Java in-place, recursively, and with streams, complete with memory diagrams, examples, and best practices.
Sort an Array in Descending Order in Java (3 Practical Methods)
Learn how to sort an array in descending order in Java using manual loops, Collections.reverseOrder(), and streams, with full examples.
Sort an Array in Ascending Order in Java
Learn how to sort an array in ascending order in Java using bubble sort, Arrays.sort(), and streams, with complexity analysis and examples.
Find the Difference Between Max and Min in a Java Array
Learn to calculate the range (max minus min) of a Java array in a single pass, with code, memory diagrams, and real-world uses.
Find Second Smallest Element in an Array in Java
Learn to find the second smallest element in a Java array in one pass without sorting, including duplicate handling and edge cases.
Find Second Largest Element in an Array in Java
Learn how to find the second largest element in a Java array in a single pass without sorting, with edge cases and interview tips.
Find Minimum Element in an Array in Java
Learn to find the minimum element in a Java array with loops, streams, and Collections, including memory diagrams and common mistakes.
Find Maximum Element in Array in Java
Learn how to find the maximum element in a Java array using loops, Arrays.sort(), Collections, and streams, with full explanations.
Find Average of Array Elements in Java
Learn to calculate the average of array elements in Java, why type casting to double matters, and how to avoid the integer division trap.