Filtering Lists

Filtering Lists

I've posted on this topic in the past, and now most post again since I'm running into the dreaded "deluge statement execution limit exceeded the maximum limit".

The records in my application contain a list. What I'm trying to do is count all the records that match values to another list. It's best to illustrate this, as lists can be confusing. Here are some records and their lists.

A (a,b,c,d)
B (b,c,f,g)
C (a,f,z)
D (a,b,z)

Count the number of records that contain either a or z. It's 3. In deluge, you do this as follows:
  1. int COUNT_RECORDS(list LIST)
  2. {
  3.     //define a list to hold values
  4.     UNIQUE_ID_LIST = List:String();
  5.     //loop thru input LIST
  6.     for each list_element in input.LIST
  7.     {
  8.         //get all records that contain one of the list elements
  9.         ID_LIST = (SUBSCRIBERS[LISTS.contains(list_element)].ID).getall();
  10.         //now add record ID numbers to another list only if they are not already in the list (i.e. unique values)
  11.         for each ID_element in ID_LIST
  12.         {
  13.             if (!UNIQUE_ID_LIST.contains(ID_element.toString()))
  14.             {
  15.                 UNIQUE_ID_LIST.add(ID_element.toString());
  16.             }
  17.         }
  18.     }
  19.     return UNIQUE_ID_LIST.size();
I have 1724 records that contain a list with can have up to 12 elements and apparently this code is hitting the limit. The issue is where the "for each ID_element in ID_LIST" part is since that could be 1724*12=20688 total steps.

Can anyone suggest a solution?

Cheers,
John Whitney
ZOHO Creator Developer