-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathexample_unique.f90
More file actions
64 lines (52 loc) · 2.03 KB
/
example_unique.f90
File metadata and controls
64 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
program example_unique
use stdlib_kinds, only: dp, sp
use stdlib_sorting, only: unique
use stdlib_string_type, only: string_type
implicit none
! Example with integer array
integer :: int_array(10) = [1, 2, 3, 3, 4, 5, 5, 6, 6, 6]
integer, allocatable :: int_unique(:)
! Example with real array
real(sp) :: real_array(8) = [3.1, 2.5, 7.2, 3.1, 2.5, 8.0, 7.2, 9.5]
real(sp), allocatable :: real_unique(:)
! Example with character array
character(len=6) :: char_array(7) = ["apple ", "banana", "cherry", "apple ", "date ", "banana", "cherry"]
character(len=6), allocatable :: char_unique(:)
! Example with string_type array
type(string_type) :: string_array(8), string_unique_sorted(4)
type(string_type), allocatable :: string_unique(:)
integer :: i
! Setup string array
string_array(1) = "apple"
string_array(2) = "banana"
string_array(3) = "cherry"
string_array(4) = "apple"
string_array(5) = "date"
string_array(6) = "banana"
string_array(7) = "cherry"
string_array(8) = "apple"
! Get unique integer values
int_unique = unique(int_array)
print *, "Unique integers:", int_unique
! Get sorted unique integer values
int_unique = unique(int_array, sorted=.true.)
print *, "Sorted unique integers:", int_unique
! Get unique real values
real_unique = unique(real_array)
print *, "Unique reals:", real_unique
! Get sorted unique real values
real_unique = unique(real_array, sorted=.true.)
print *, "Sorted unique reals:", real_unique
! Get unique character values
char_unique = unique(char_array)
print *, "Unique strings:"
do i = 1, size(char_unique)
print *, char_unique(i)
end do
! Get unique string_type values (sorted)
string_unique = unique(string_array, sorted=.true.)
print *, "Sorted unique string_type values:"
do i = 1, size(string_unique)
print *, string_unique(i)
end do
end program example_unique