-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnique-Array.gdl
50 lines (44 loc) · 1.01 KB
/
Unique-Array.gdl
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
!--- Make An Array Unique in GDL ---!
! (= get rid of duplicates inside an array)
! ["A", "A", "B", "C", "A", "C", "B"] => ["A", "B", "C"]
! Caution: We don't do any kind of sorting here; the input determines ordering of the output.
! Go to https://github.com/runxel/GDL-playground for more.
!>>> Test Array:
dim array[]
array[1] = "A"
array[2] = "A"
array[3] = "B"
array[4] = "C"
array[5] = "A"
array[6] = "C"
array[7] = "B"
size = vardim1(array)
dim freq[]
! init with -1
for f = 1 to size
freq[f] = -1
next f
for i = 1 to size
count = 1
for j = i+1 to size
if array[i] = array[j] then
count = count+1
freq[j] = 0
endif
next j
if freq[i] # 0 then
freq[i] = count
endif
next i
dim unique[]
unc = 1
for l = 1 to size
if freq[l] > 0 then
! if you assert that freq[l] = 1 then you can have a truly unique set
! => list consist only of items with no dups
unique[unc] = array[l]
unc=unc+1
endif
next l
!>>> TEST:
print unique