3

I had written the code for finding duplicate elements in C but now I am stuck at implementing the same code in freemarker Can anyone help?

int n, a[10], b[10], count = 0, c, d;

   printf("Enter number of elements in array\n");
   scanf("%d",&n);

   printf("Enter %d integers\n", n);
   for(c=0;c<n;c++)
      scanf("%d",&a[c]);

   for(c=0;c<n;c++)
   {
      for(d=0;d<count;d++)
      {
         if(a[c]==b[d])
            break;
      }
      if(d==count)
      {
         b[count] = a[c];
         count++;
      }
   }

   printf("Array obtained after removing duplicate elements\n");         
   for(c=0;c<count;c++)
      printf("%d\n",b[c]);
marg
  • 91
  • 1
  • 12
  • One tag is C, the code seems C. But freemarker is a java thing, isn't it? Are you porting this C->java? Or trying to use a java engine from C? – Yunnosch Aug 08 '18 at 05:12
  • I needed to implement this logic in netsuite advanced pdf templates – marg Aug 08 '18 at 05:19
  • See https://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist – Ori Marko Aug 08 '18 at 05:46
  • The root of the problem is that FreeMarker is not really a programming language... it's a template language. Think of printf but much more powerful. So, if the data-model content itself can't be changed, can you at least add some helper Java classes there? The template can pull in and instantiate such classes itself, they only have to exist in your "classpath" somehow. – ddekany Aug 08 '18 at 07:40
  • @ddekany but still we can implement this no?? – marg Aug 08 '18 at 11:24
  • @marg It can be implemented inside the template, but it can get really slow, as the template language doesn't have true mutable lists. – ddekany Aug 09 '18 at 07:34

1 Answers1

8

You can use freemarker sequences. Probably not super efficient but I've used this to group close to max size lines on invoices and such.

<#assign seen_style = []>
<#list record.item?sort_by("custcol_stylesort")  as lineitem>
   <#assign groupId = lineitem.item>
   <#if seen_style?seq_contains(groupId)> <!-- no if body is intentional; skips seen style -->
   <#else>
     <#assign seen_style = seen_style + [groupId]>
     <p>Do something with ${groupId}</p>
   </#if>
</#list>
bknights
  • 14,408
  • 2
  • 18
  • 31