Java Programs›Arrays

🔢Java Arrays Programs

Array problems are where interviewers check whether you can move from a brute-force loop to an efficient one-pass solution. These programs cover searching, sorting, two-pointer tricks and 2D matrices, each with its time and space complexity.68 programs · each with full code, output and a step-by-step explanation

Most Asked in Interviews

🔍
Beginner
01beginner
Add Two Matrices
Matrix

Adds two matrices of the same size by summing the values at each matching row and column.

View Program →
02beginner
Average Array Elements
Array

Calculates the average of an array's elements.

View Program →
03beginner
Check Array Is Sorted
Array

Determines whether an array's elements are in ascending order.

View Program →
04beginner
Compare Two Arrays
Array Manipulation

Checks whether two arrays hold the same elements in the same order.

View Program →
05beginner
Concatenate Arrays
Array Manipulation

Joins two arrays end to end into a single array.

View Program →
06beginner
Copy Array
Array Manipulation

Creates an independent copy of an array.

View Program →
07beginner
Count Even Elements in an Array
Array

Counts how many even numbers appear in an array.

View Program →
08beginner
Count Negative Elements in an Array
Array

Counts how many negative numbers appear in an array.

View Program →
09beginner
Count Odd Elements in an Array
Array

Counts how many odd numbers appear in an array.

View Program →
10beginner
Count Positive Elements in an Array
Array

Counts how many positive numbers appear in an array.

View Program →
11beginner
Count Zeros in an Array
Array

Counts how many zero values appear in an array.

View Program →
12beginner
Create and Print Array
Array

Declares an array and prints every element it holds.

View Program →
13beginner
Find Maximum Element
Array

Scans an array to find its largest value.

View Program →
14beginner
Find Second Largest Element in an Array
Array

Scans an array to find its second-largest value.

View Program →
15beginner
Find Second Smallest Element in an Array
Array

Scans an array to find its second-smallest value.

View Program →
16beginner
Find Smallest Element in an Array
Array

Scans an array to find its smallest value.

View Program →
17beginner
Find Sum of Matrix Diagonals
Matrix

Adds up the primary and secondary diagonals of a square matrix separately.

View Program →
18beginner
Find Third Largest Element in an Array
Array

Scans an array to find its third-largest value.

View Program →
19beginner
Linear Search
Searching

Searches an array for a target value by checking every element in order.

View Program →
20beginner
Move Zeros to End
Array Manipulation

Moves every zero in an array to the end, keeping the non-zero elements in order.

View Program →
21beginner
Read Array From User
I/O

Reads an array's size and elements from the console.

View Program →
22beginner
Remove Duplicate Elements
Array Manipulation

Removes repeated elements from an array, keeping each one's first occurrence.

View Program →
23beginner
Reverse an Array
Array Manipulation

Reverses the elements of an integer array in place.

View Program →
24beginner
Rotate Array Left
Array Manipulation

Rotates an array's elements to the left by a given number of positions.

View Program →
25beginner
Rotate Array Right
Array Manipulation

Rotates an array's elements to the right by a given number of positions.

View Program →
26beginner
Separate Even and Odd
Array

Splits an array's elements into separate lists of even and odd numbers.

View Program →
27beginner
Separate Positive and Negative
Array

Splits an array's elements into separate lists of positive and negative numbers.

View Program →
28beginner
Sort an Array in Ascending Order
Sorting

Sorts an array into ascending order using the built-in sort method.

View Program →
29beginner
Sort an Array in Descending Order
Sorting

Sorts an array into descending order.

View Program →
30beginner
Sum Array Elements
Array

Adds up every element of an array.

View Program →
31beginner
Transpose a Matrix
Matrix

Flips a matrix over its main diagonal so that every row becomes a column.

View Program →
Intermediate
32intermediate
Binary Search
Searching

Searches a sorted array for a target value by repeatedly halving the search range.

View Program →
33intermediate
Bubble Sort
Sorting

Sorts an array by repeatedly swapping adjacent out-of-order elements.

View Program →
34intermediate
Counting Sort
Sorting

Sorts small-range integers without comparisons by tallying how often each value appears.

View Program →
35intermediate
Find Duplicate Elements
Array

Finds every element that appears more than once in an array.

View Program →
36intermediate
Find Equilibrium Index
Array

Finds the index where the sum of elements to its left equals the sum to its right.

View Program →
37intermediate
Find Frequency of Elements
Array

Counts how many times each element appears in an array.

View Program →
38intermediate
Find Intersection of Arrays
Array

Finds every element that appears in both of two arrays.

View Program →
39intermediate
Find Leaders in an Array
Array

Finds every element that is greater than all the elements to its right.

View Program →
40intermediate
Find Least Frequent Element
Array

Finds the element that appears least often in an array.

View Program →
41intermediate
Find Local Minimum in an Array
Array

Finds an element that is smaller than both of its neighbors.

View Program →
42intermediate
Find Maximum Difference Between Two Elements
Array

Finds the largest value obtainable by subtracting an earlier element from a later one.

View Program →
43intermediate
Find Minimum Difference Between Two Elements
Array

Finds the smallest absolute difference between any two elements in an array.

View Program →
44intermediate
Find Minimum Subarray Sum
Array

Finds the smallest sum achievable by any contiguous subarray.

View Program →
45intermediate
Find Missing Number
Array

Finds the single missing number from a sequence of 1 to n.

View Program →
46intermediate
Find Most Frequent Element
Array

Finds the element that appears most often in an array.

View Program →
47intermediate
Find Pair With Given Sum
Array

Finds two numbers in an array that add up to a target sum.

View Program →
48intermediate
Find Peak Element in an Array
Array

Finds an element that is greater than both of its neighbors.

View Program →
49intermediate
Find Repeated Number
Array

Finds the single number that repeats in an array of n + 1 values from 1 to n.

View Program →
50intermediate
Find Subarray With Given Sum
Array

Finds a contiguous subarray whose elements add up to a target sum.

View Program →
51intermediate
Find Union of Arrays
Array

Finds every distinct element present in either of two arrays.

View Program →
52intermediate
Find Unique Element
Array

Finds the single element that appears once while every other element appears exactly twice.

View Program →
53intermediate
Insertion Sort
Sorting

Sorts an array by inserting each element into its correct position among the ones already sorted.

View Program →
54intermediate
Merge Sort
Sorting

Sorts an array by splitting it in half, sorting each half recursively and merging the sorted halves.

View Program →
55intermediate
Merge Two Arrays
Array Manipulation

Merges two sorted arrays into a single sorted array.

View Program →
56intermediate
Move Negative Numbers to Beginning
Array Manipulation

Rearranges an array in place so every negative number comes before the positives.

View Program →
57intermediate
Multiply Two Matrices
Matrix

Multiplies two matrices by pairing each row of the first with each column of the second.

View Program →
58intermediate
Print Matrix in Spiral Order
Matrix

Reads a matrix clockwise from the outside in, peeling off one ring of cells at a time.

View Program →
59intermediate
Quick Sort
Sorting

Sorts an array by partitioning it around a pivot, then sorting the smaller and larger sides recursively.

View Program →
60intermediate
Rotate Array by K Positions
Array Manipulation

Rotates an array by k positions in place using the reversal algorithm.

View Program →
61intermediate
Selection Sort
Sorting

Sorts an array by repeatedly selecting the smallest remaining element.

View Program →