Sort in Ascending Order (Bubble Sort)
public class SortAscending {
public static void main(String[] args) {
int[] numbers = {45, 12, 78, 3, 25};
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = 0; j < numbers.length - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
System.out.print("Sorted Array in Ascending Order: ");
for (int num : numbers)
System.out.print(num + " ");
}
}
Logic
Bubble Sort repeatedly compares adjacent elements and swaps them whenever they are in the wrong order. During every iteration, the largest element moves to the end of the array.
Time Complexity: O(n²)
For production code, Java provides:
Arrays.sort(numbers);
Sort in Descending Order
Use the same Bubble Sort logic while changing the comparison condition.
Replace:
if (numbers[j] > numbers[j + 1])
with:
if (numbers[j] < numbers[j + 1])
This arranges the elements in descending order.
Reverse an Array
public class ReverseArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int start = 0;
int end = numbers.length - 1;
while (start < end) {
int temp = numbers[start];
numbers[start] = numbers[end];
numbers[end] = temp;
start++;
end--;
}
System.out.print("Reversed Array: ");
for (int num : numbers)
System.out.print(num + " ");
}
}
Logic
The Two-Pointer approach swaps the first and last elements, then moves both pointers toward the center until they meet.
The array is reversed in place, without creating another array.
Time Complexity: O(n)
Space Complexity: O(1)
Find Duplicate Elements
The brute-force solution compares every element with every remaining element using nested loops.
Whenever:
arr[i] == arr[j]
the element is identified as a duplicate.
Time Complexity: O(n²)
Find Duplicates Using HashSet
public class FindDuplicateOptimized {
public static void main(String[] args) {
int[] arr = {2, 4, 5, 2, 7, 4, 9};
HashSet<Integer> set = new HashSet<>();
System.out.println("Duplicate elements:");
for (int num : arr) {
if (!set.add(num)) {
System.out.println(num);
}
}
}
}
Logic
A HashSet stores only unique values.
When:
set.add(num)
returns false, the value already exists in the set, meaning it is a duplicate.
Time Complexity: O(n)
Remove Duplicates
Use:
- HashSet
- LinkedHashSet (to preserve insertion order)
to store only unique values.
Since a Set automatically rejects duplicate elements, duplicate values are removed while collecting the data.
Time Complexity: O(n)
Count Frequency of Each Element
public class CountFrequencyOfElements {
public static void main(String[] args) {
int[] arr = {2, 4, 5, 2, 7, 4, 4};
boolean[] visited = new boolean[arr.length];
System.out.println("Frequency of each element:");
for (int i = 0; i < arr.length; i++) {
if (visited[i])
continue;
int count = 1;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = true;
}
}
System.out.println(arr[i] + " -> " + count + " times");
}
}
}
Logic
A visited[] array prevents counting the same element multiple times.
The optimized alternative uses a HashMap for frequency counting.
Find Unique & First Duplicate Elements
Find Unique Elements
Unique elements are values that appear only once in the array.
Determine the frequency of every element and print those whose count is equal to 1.
Find the First Duplicate
Traverse the array from left to right using a HashSet.
The first element whose:
set.add(value)
returns false is the first duplicate encountered.
Frequently Asked Questions
How do you find duplicates efficiently in an array?
Use a HashSet. If set.add(value) returns false, the element already exists and is therefore a duplicate.
This approach runs in O(n) time.
How do you reverse an array without creating another array?
Use the Two-Pointer approach.
Swap the first and last elements, move both pointers inward, and continue until they meet.
Time Complexity: O(n)
Space Complexity: O(1)
How do you count the frequency of array elements?
You can use either:
- A
visited[]array with nested loops - A
HashMapusinggetOrDefault()
How does Bubble Sort work?
Bubble Sort repeatedly compares adjacent elements and swaps them whenever they are out of order.
After every pass, the largest element moves to its correct position.
Time Complexity: O(n²)
How do you find the first duplicate element?
Traverse the array using a HashSet.
The first element whose add() operation returns false is the first duplicate.