![]() |
VOOZH | about |
Arrays are a fundamental data structure in Java that stores data of the same type in contiguous memory locations. Removing duplicate elements from an array is a common operation that can be easily accomplished using sets. However, in this article, we will learn how to remove duplicates from an array in Java without using a set, in an efficient manner.
Input: arr[] ={ 1,2,2,3,3,3,4,5,5,6}
Output: arr[]= {1,2,3,4,5,6}
To remove duplicate elements from an Array without using a Set in Java, we can follow the below algorithm:
The following program demonstrates how we can remove duplicates from an array without using a set in Java:
Original Array: [1, 2, 3, 4, 5, 6, 4, 5, 5, 6] Array without Duplicates: [1, 2, 3, 4, 5, 6]
Time Complexity: O(N logN), where N is the size of the array.
Auxiliary Space: O(K), where K is the size of the array without duplicate elements.