Show duplicates in internal table in ABAP
Show duplicates in internal table in ABAP. This approach ensures that duplicates are detected and stored in a separate internal table for further processing.

To identify duplicates in an internal table in ABAP, you can use a combination of sorting and looping. Here’s an example that shows how to find duplicate entries in an internal table:
DATA: BEGIN OF itab OCCURS 0, field1 TYPE i, field2 TYPE c LENGTH 10, END OF itab, wa LIKE LINE OF itab. * Add sample data to internal table itab-field1 = 1. itab-field2 = 'A'. APPEND itab. itab-field1 = 2. itab-field2 = 'B'. APPEND itab. itab-field1 = 1. itab-field2 = 'A'. APPEND itab. itab-field1 = 3. itab-field2 = 'C'. APPEND itab. itab-field1 = 2. itab-field2 = 'B'. APPEND itab. * Sort internal table by the fields you want to check duplicates for SORT itab BY field1 field2. * Compare current record with
In ABAP, you can use a simple logic to identify and show duplicates in an internal table. One common approach is to use SORT
and DELETE ADJACENT DUPLICATES
to flag or separate the duplicates. However, if you want to retain the duplicates for further processing or display, a different method using a loop can be used.
Here’s an example that shows how to find and display duplicates in an internal table:
TYPES: BEGIN OF ty_data, id TYPE i, name TYPE string, END OF ty_data. DATA: lt_table TYPE TABLE OF ty_data, ls_table TYPE ty_data, lt_duplicates TYPE TABLE OF ty_data. " Populate the internal table with test data APPEND VALUE #( id = 1 name = 'John' ) TO lt_table. APPEND VALUE #( id = 2 name = 'Jane' ) TO lt_table. APPEND VALUE #( id = 3 name = 'John' ) TO lt_table. APPEND VALUE #( id = 4 name = 'Alice' ) TO lt_table. APPEND VALUE #( id = 5 name = 'Jane' ) TO lt_table. APPEND VALUE #( id = 6 name = 'John' ) TO lt_table. " Sort the internal table by name or ID to find duplicates SORT lt_table BY name. " Loop through the table to find duplicates LOOP AT lt_table INTO ls_table. AT END OF name. IF sy-tabix - sy-tabbix > 1. LOOP AT lt_table FROM sy-tabbix TO sy-tabix INTO ls_table. APPEND ls_table TO lt_duplicates. ENDLOOP. ENDIF. ENDAT. ENDLOOP. " Display the duplicates LOOP AT lt_duplicates INTO ls_table. WRITE: / 'Duplicate:', ls_table-id, ls_table-name. ENDLOOP.
Explanation:
- Internal Table (
lt_table
): This table stores the data records. - Sorting: The table is sorted by the column where you expect to find duplicates (in this case,
name
). - Loop with AT END OF: This checks if a group of records has the same key (name in this case). If more than one record is found for a particular key, those entries are identified as duplicates.
- Duplicate Handling: If duplicates are found, they are collected into the
lt_duplicates
table. - Display: The duplicates are then displayed using a
LOOP
.
This approach ensures that duplicates are detected and stored in a separate internal table for further processing.
REPORT duplicates. DATA: BEGIN OF lt_duplicates OCCURS 0, f2(10), f3(10), END OF lt_duplicates, it_test TYPE TABLE OF ztest WITH HEADER LINE, i TYPE i. SELECT DISTINCT f2 f3 FROM ztest INTO TABLE lt_duplicates. LOOP AT lt_duplicates. IF f2 = lt_duplicates-f2 AND f3 = lt_duplicates-f3. ENDIF. i = LINES( it_test ). IF i > 1. LOOP AT it_test. WRITE :/ it_test-f1,it_test-f2,it_test-f3. ENDLOOP. ENDIF. ENDLOOP.