Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Array Element Frequency

Status
Not open for further replies.

simpelli

Programmer
Jul 2, 2003
30
US
I've have a string array to hold certain field values whilereadingrecords. I need to be able to count the number of times a specific string exists in the array.

So if the array holds
"AA"
"BB"
"CC"
"AA"
"DD"
"AA"

I need to know that "AA" occours more than once - that's actually the key. If an Item is in there only once, a certain status must be displayed, if anything more than one, a different status.

I have no problem creating, reading, counting, displaying a specific element, but I can't figure out how to determine the frequency of a specific element.

Thanks in advance,

-SI
 
So frequency isn't the key, just whether something exists more than once, right? I'm not sure why if you know how to count why this is a problem.

Since you've given no indication how you might be using this fact, I'll just write a double loop that counts them:

numbervar x;
numbervar y;
numbervar mycount := 0;
numbervar array myarray := [1,2,3,1,2];
numbervar array counts;
redim counts[ubound(myarray)];
for x := 1 to ubound(myarray) do(
for y := 1 to ubound(myarray) do(
if myarray[y] = myarray[X] then
counts[x] := counts[x]+1
);
);
counts[2] // displays the count of the second element in the array

-k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top