1

I am loading an array in a RPGLE program based on a business logic which may result in duplicate data in the array.

I want to know firstly how can I detect the duplication.

And finally I would like to know how can I remove the duplication in the array.

jmarkmurphy
  • 11,030
  • 31
  • 59
Kunal Roy
  • 729
  • 3
  • 11

2 Answers2

7

You could use %LOOKUP to see if the entry is already in the array before you add it.

if %lookup(newValue : array : 1 : numElems) = 0;
   // the element is not in the array yet
   numElems += 1;
   array(numElems) = newValue;
endif;
Barbara Morris
  • 3,195
  • 8
  • 10
4

detecting duplicate entries in arrays is quite simple:

0.) sort the array 
1.) loop through the array and check if the current item matches the previous item 
2.) if so, then remove the current item or 

or you can

0.) create a temporary array
1.) loop through the original array and check if the current item is already contained in the temp arrayitem 
2.) if not, then copy the current item into the temp array
3.) clear the original array and copy them from the temp to the oiginal array

Here you have a SO question regarding the topic in general: Array remove duplicate elements And here is a thread about doing this in RPGLE: https://code400.com/forum/forum/iseries-programming-languages/rpg-rpgle/7270-check-for-duplicates-in-array-of-size-50

If the order of entries in the original array has to be the same after removing the duplicate items, then the second approach would be better

Edit: Made a mistake, second approach does not need any sorting (removed this point)

Radinator
  • 1,048
  • 18
  • 58
  • 2
    for your first approach, how do you recommend doing step 3 ("remove the current item")? I can think of a couple of ways to do that, but they are both quite complex and/or would perform poorly. – Barbara Morris Feb 08 '21 at 13:28
  • I would just "delete" these items (setting them to a NULL value or something). Then you would have to either take all the remaining items and move them 1 index prior (which makes the compexity of the algo very high) or you leave them "nulled-out" and after cleaning the original array transfer the "non nullled-out" value to another array, clear the orignal array and transfer them back. So I think you can't come around a second array – Radinator Feb 09 '21 at 12:10
  • But after thinking about this, I can surely recommend approach #2 in this situation. Like Barbara Morris code below/above shows. – Radinator Feb 09 '21 at 12:13
  • Radinator, your idea about nulling-out the deleted items could work. If you set the unwanted elements to *HIVAL, you could sort the array again after the loop, and then reduce your "number of elements" variable by the number of "deleted" elements. – Barbara Morris Feb 09 '21 at 21:13
  • Yes, this was the approach :D But as said in my post, if the order of elements has to be the same as the "input", you should iterate over the elements and copy them to the temp array only if they aren't already contained in the temp arry – Radinator Feb 09 '21 at 22:42