title |
---|
unique function |
[TOC]
This function returns an array containing only the unique values extracted from an input array. This is useful for removing duplicates from datasets and finding the distinct elements in a collection.
The unique
function is currently in experimental status.
Version | Change |
---|---|
v0.1.0 | Initial functionality in experimental status |
This function has been designed to handle arrays of different types, including intrinsic numeric types, character arrays, and string_type
arrays. The function should be efficient while maintaining an easy-to-use interface.
! Get unique values from an integer array
integer :: x(5) = [1, 2, 3, 3, 4]
integer, allocatable :: y(:)
y = unique(x) ! y will be [1, 2, 3, 4]
! Get sorted unique values from a real array
real :: a(8) = [3.1, 2.5, 7.2, 3.1, 2.5, 8.0, 7.2, 9.5]
real, allocatable :: b(:)
b = unique(a, sorted=.true.) ! b will be [2.5, 3.1, 7.2, 8.0, 9.5]
pure function unique(array, sorted) result(unique_values)
<type>, intent(in) :: array(:)
logical, intent(in), optional :: sorted
<type>, allocatable :: unique_values(:)
end function unique
where <type>
can be any of:
integer(int8)
,integer(int16)
,integer(int32)
,integer(int64)
real(sp)
,real(dp)
,real(xdp)
,real(qp)
complex(sp)
,complex(dp)
,complex(xdp)
,complex(qp)
character(len=*)
type(string_type)
array
: Array whose unique values need to be extracted.
sorted
(optional): Whether the output vector needs to be sorted or not. Default is .false.
.
The function returns an allocatable array containing only the unique values from the input array.
If sorted
is .true.
, the returned array will be sorted in order of non-decreasing values.
If sorted
is .false.
(the default), the order of elements is unspecified but generally reflects the order of first appearance of each unique value in the input array.
program example_unique_integers
use stdlib_sorting, only: unique
implicit none
integer :: data(10) = [1, 2, 3, 3, 4, 5, 5, 6, 6, 6]
integer, allocatable :: unique_values(:)
! Get unique values
unique_values = unique(data)
! Print the results
print *, "Original array: ", data
print *, "Unique values: ", unique_values
end program example_unique_integers
Expected output:
Original array: 1 2 3 3 4 5 5 6 6 6
Unique values: 1 2 3 4 5 6
program example_unique_reals
use stdlib_kinds, only: sp
use stdlib_sorting, only: unique
implicit none
real(sp) :: data(8) = [3.1, 2.5, 7.2, 3.1, 2.5, 8.0, 7.2, 9.5]
real(sp), allocatable :: unique_values(:)
! Get unique values in sorted order
unique_values = unique(data, sorted=.true.)
! Print the results
print *, "Original array: ", data
print *, "Sorted unique values: ", unique_values
end program example_unique_reals
Expected output:
Original array: 3.1 2.5 7.2 3.1 2.5 8.0 7.2 9.5
Sorted unique values: 2.5 3.1 7.2 8.0 9.5
program example_unique_strings
use stdlib_sorting, only: unique
implicit none
character(len=6) :: data(7) = ["apple ", "banana", "cherry", "apple ", "date ", "banana", "cherry"]
character(len=6), allocatable :: unique_values(:)
integer :: i
! Get unique values
unique_values = unique(data)
! Print the results
print *, "Original array:"
do i = 1, size(data)
print *, data(i)
end do
print *, "Unique values:"
do i = 1, size(unique_values)
print *, unique_values(i)
end do
end program example_unique_strings
The implementation uses a sorting-based approach to identify unique elements efficiently. When sorted=.true.
, the algorithm sorts the input array and then identifies adjacent duplicate elements. When sorted=.false.
, the function still uses sorting internally but ensures that the order of first appearance is preserved.
Future versions might include additional features:
- Return the indices of the first occurrence of each unique element
- Return indices that can reconstruct the original array from the unique elements
- Support for multi-dimensional arrays
- Tolerance parameter for floating-point comparisons
sort
- Sorts an array in ascending or descending ordersort_index
- Creates index array that would sort an arrayord_sort
- Performs a stable sort on an array