ABAP: Finding duplicates in internal table

With Release 7.40 SP08 you can use the GROUP BY statement in a LOOP to find duplicates. In this example we are looking for duplicates with the same MATNR.

LOOP AT lt_tab INTO ls_tab GROUP BY ( matnr = ls_artinfo-matnr size = GROUP SIZE ) ASCENDING REFERENCE INTO DATA(ls_ref).
  IF ls_ref->*-size > 1.
    "Duplicate found. key in in ls_ref->matnr and the amount is in ls_ref->size
  ENDIF.
ENDLOOP.
1
2
3
4
5