Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete repeated entries in array
#1
Hello,

I have an array that have multiple repeated information and I want to form a new array with only single entries of what it contains.
Examlpe:

Array a = [1], [2], [2], [3], [3], [4], [5], [5]
Array a_new = [1], [2], [3], [4], [5]

How can I do this? Thanks a lot!
#2
Macro Macro2373
Code:
Copy      Help
out
ARRAY(int) a b
int i j

;create array a for testing
out "array a:"
a.create(10)
for(i 0 a.len) a[i]=RandomInt(0 a.len/2); out a[i]

;create array b from unique elements of array a
for i 0 a.len
,int& r=a[i]
,for(j b.len-1 -1 -1) if(b[j]=r) break
,if(j<0) b[]=r

out "array b:"
for(i 0 b.len) out b[i]
#3
Same thing here, just use requestor's sample:

Macro Unique Array 1
Code:
Copy      Help
ARRAY(str) a="1[]2[]2[]3[]3[]4[]5[]5"
int i j
ARRAY(int) b

;create array b from unique elements of array a
for i 0 a.len
,for(j b.len-1 -1 -1) if(b[j]=val(a[i])) break
,if(j<0) b[]=val(a[i])

out "array a:"
for(i 0 a.len) out a[i]
out ""
out "array a_new:"
for(i 0 b.len) out b[i]
#4
I saw that this one only worked for integer array (or a string array of integers!), so I made these functions which work for either str or int array and returns total unique elements

(I would have combined into one function by passing the array (either int or str) as safearray and then testing with GetArrayType from http://www.quickmacros.com/forum/showthr...0#pid25350 but I couldn't figure out once I passed the array safely and tested it, how to convert it back into ARRAY(str) or ARRAY(int).

Anyway hope this is useful,
S

Macro GetUniqueArrayElementsExample
Code:
Copy      Help
out
ARRAY(int) a

a[] = 1
a[] = 2
a[] = 2
a[] = 3
a[] = 3
a[] = 3
a[] = 4
a[] = 5
a[] = 5


ARRAY(str) s = "red[]red[]blue[]blue[]green[]yellow[]yellow"

_i = GetUniqueArrayElements_Int(a)
out "[]"
out F"Number of unique integer elements = {_i}"
out "[]----------[]"
_i = GetUniqueArrayElements_Str(s)
out "[]"
out F"Number of unique str elements = {_i}"

Function GetUniqueArrayElements_Int
Code:
Copy      Help
function ARRAY(int)&arrToUnDupe


int i j
ARRAY(int) b

for i 0 arrToUnDupe.len
,for(j b.len-1 -1 -1) if(b[j]=arrToUnDupe[i]) break
,if(j<0) b[]=arrToUnDupe[i]

out "array arrToUnDupe:"
for(i 0 arrToUnDupe.len) out arrToUnDupe[i]
out ""
out "array arrToUnDupe_unique:"
for(i 0 b.len) out b[i]
ret b.len

Function GetUniqueArrayElements_Str
Code:
Copy      Help
function ARRAY(str)&arrToUnDupe

int i j
ARRAY(str) b

;create array b from unique elements of array a
for i 0 arrToUnDupe.len
,for(j b.len-1 -1 -1) if(b[j]=arrToUnDupe[i]) break
,if(j<0) b[]=arrToUnDupe[i]

out "array arrToUnDupe:"
for(i 0 arrToUnDupe.len) out arrToUnDupe[i]
out ""
out "array arrToUnDupe_unique:"
for(i 0 b.len) out b[i]
ret b.len
#5
You can use this to combine it into one function.

Function GetUniqueArrayElements
Code:
Copy      Help
function `v
int uniqueElement
sel v.vt
,case VT_I4|VT_ARRAY
,,ARRAY(int)& a=v.parray
,,uniqueElement = GetUniqueArrayElements_Int(a)
,case VT_BSTR|VT_ARRAY
,,ARRAY(str) s=v.parray
,,uniqueElement = GetUniqueArrayElements_Str(s)
,case else end ERR_BADARG
ret uniqueElement

Macro GetUniqueArrayElements Test
Code:
Copy      Help
out
ARRAY(int) a

a[] = 1
a[] = 2
a[] = 2
a[] = 3
a[] = 3
a[] = 3
a[] = 4
a[] = 5
a[] = 5

ARRAY(str) b = "red[]red[]blue[]blue[]green[]yellow[]yellow"

_i = GetUniqueArrayElements(a)
out "[]"
out F"Number of unique integer elements = {_i}"
out "[]----------[]"
_i = GetUniqueArrayElements(b)
out "[]"
out F"Number of unique str elements = {_i}"
Basicallly it's just a function wrapper only. Underneath of it you still have to define 2 separate functions for dealing with int array and str array.
#6
Quote:v.parray

that's perfect - the exact piece I was missing!
Thanks,
S


Forum Jump:


Users browsing this thread: 1 Guest(s)