Generally programmers should think logically and write logical steps first before writing Java
programs. In this context can you give logical approach, steps and clear Java program to execute Java
*program for removing duplicates and print it separately of an array [10, 2, 15, 30, 7, 10, 25, 3, 2,
5, 17, 20, 3]. Also the same program to print the array without duplicates separately.
First Method (HashSet)
Logical Approach:
1. Initialize Data – Start with the given array [10, 2, 15, 30, 7, 10, 25, 3, 2, 5, 17, 20, 3].
2. Use a Data Structure for Uniqueness – Utilize a Set (e.g., HashSet) since it stores unique
elements.
3. Traverse the Array – Iterate through the array while adding elements to the set.
4. Separate Duplicates – Maintain another structure (List) to track duplicate elements.
5. Display Output – Print unique values first, then the duplicates separately.
Java Program:
import [Link].*;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {10, 2, 15, 30, 7, 10, 25, 3, 2, 5, 17, 20, 3};
Set<Integer> uniqueSet = new HashSet<>();
List<Integer> duplicateList = new ArrayList<>();
for (int num : arr) {
if () { // If adding fails, it's a duplicate
[Link](num);
// Printing the array without duplicates
[Link]("Array without duplicates: " + uniqueSet);
// Printing duplicates separately
[Link]("Duplicates: " + duplicateList);
}
Expected Output:
Array without duplicates: [2, 3, 5, 7, 10, 15, 17, 20, 25, 30]
Duplicates: [10, 2, 3]
Explanation:
HashSet ensures each element appears only once.
ArrayList captures elements that are encountered more than once.
The output shows the array with duplicates removed separately from the duplicate elements
themselves.
This method is both efficient and simple! Let me know if you’d like modifications or enhancements.
--------------------------------------------------------------------------------------------------------------------------------
2) Another Way (Array List)
If you want to remove duplicates and find duplicate elements without using HashMap or Hashtable,
you can achieve this with simple loops and arrays. Let’s go for a logical approach that ensures
uniqueness without relying on hashing.
Logical Approach:
1. Initialize an empty array or list to store unique elements.
2. Traverse the array and check if the element is already present in the unique list:
o If it is not present, add it.
o If it is present, add it to a duplicate list.
3. Display the results – one list will contain distinct elements, and another will show duplicate
elements.
Java Program Without HashMap or Hashtable
import [Link];
import [Link];
public class RemoveDuplicatesWithoutHashing {
public static void main(String[] args) {
int[] arr = {10, 2, 15, 30, 7, 10, 25, 3, 2, 5, 17, 20, 3};
List<Integer> uniqueList = new ArrayList<>();
List<Integer> duplicateList = new ArrayList<>();
for (int i = 0; i < [Link]; i++) {
boolean isDuplicate = false;
// Check if the element is already in the unique list
for (int j = 0; j < [Link](); j++) {
if (arr[i] == [Link](j)) {
isDuplicate = true;
break;
if (isDuplicate) {
if () { // Prevent multiple duplicate entries
[Link](arr[i]);
} else {
[Link](arr[i]);
// Printing the array without duplicates
[Link]("Array without duplicates: " + uniqueList);
// Printing duplicates separately
[Link]("Duplicates: " + duplicateList);
}
Expected Output:
Array without duplicates: [10, 2, 15, 30, 7, 25, 3, 5, 17, 20]
Duplicates: [10, 2, 3]
Explanation:
Instead of using a HashMap or HashSet, we manually search through the list using a nested
loop.
The uniqueList keeps track of distinct elements.
The duplicateList only stores elements that appear more than once.
A nested loop checks for existing elements, ensuring uniqueness without hashing.
This approach is a bit less efficient (O(n²) complexity) compared to hashing, but it works
purely with lists.
-----------------------------------------------------------------------------------------------------------------------------
3) Another Way [Simple Array and loop]
Even we can write the same java program without List method also simply using array, for loop and If
then condition.
Logical Approach:
1. Create two arrays:
o One for storing unique elements.
o Another for tracking duplicate elements.
2. Traverse the original array:
o Use a nested loop to check if an element already exists in the unique array.
o If the element is not present, add it to the unique array.
o If the element is found, add it to the duplicate array (only if it’s not already
recorded).
3. Display the unique elements and duplicates separately.
Java Program Without List, HashMap, or HashSet
public class RemoveDuplicatesArrayOnly {
public static void main(String[] args) {
int[] arr = {10, 2, 15, 30, 7, 10, 25, 3, 2, 5, 17, 20, 3};
int n = [Link];
int[] uniqueArr = new int[n]; // To store unique elements
int[] duplicateArr = new int[n]; // To store duplicate elements
int uniqueCount = 0;
int duplicateCount = 0;
for (int i = 0; i < n; i++) {
boolean isDuplicate = false;
// Check if arr[i] is already in uniqueArr
for (int j = 0; j < uniqueCount; j++) {
if (arr[i] == uniqueArr[j]) {
isDuplicate = true;
break;
if (isDuplicate) {
// Check if arr[i] is already in duplicateArr before adding
boolean alreadyAdded = false;
for (int k = 0; k < duplicateCount; k++) {
if (arr[i] == duplicateArr[k]) {
alreadyAdded = true;
break;
if (!alreadyAdded) {
duplicateArr[duplicateCount++] = arr[i];
} else {
uniqueArr[uniqueCount++] = arr[i];
}
// Print unique elements
[Link]("Array without duplicates: ");
for (int i = 0; i < uniqueCount; i++) {
[Link](uniqueArr[i] + " ");
[Link]();
// Print duplicate elements
[Link]("Duplicates: ");
for (int i = 0; i < duplicateCount; i++) {
[Link](duplicateArr[i] + " ");
[Link]();
Expected Output:
Array without duplicates: 10 2 15 30 7 25 3 5 17 20
Duplicates: 10 2 3
Explanation:
No Lists or HashMaps—we use two simple arrays.
The first array (uniqueArr) stores distinct elements.
The second array (duplicateArr) tracks duplicate elements.
Nested loops manually check for duplicates, ensuring elements are added only when
necessary.
The approach follows O(n²) complexity due to nested iteration.