Delete duplicates in a loop?

3.1k views Asked by At

I have a table with some data:

101
101
102
101
102
103
103

and I need to delete duplicates like this:

101
102
101
102
103

but in this case I cant use delete adjacent duplicates.

What can I do?

4

There are 4 answers

2
Gert Beukema On
DATA: LS_PREV LIKE LINE OF TAB.
" Set LS_PREV to some value you know will not occur in the table
LOOP AT TAB INTO LS_TAB.
  IF LS_TAB EQ LS_PREV.
    DELETE TAB.
  ENDIF.
  LS_PREV = LS_TAB.
ENDLOOP.

or as vwegert points out below:

DELETE ADJACENT DUPLICATES FROM TAB.
0
ChristianR On
  1. Do not sort the internal table to keep the order of the entries.
  2. Use DELETE ADJACENT DUPLICATES FROM (tableName).

Result: Only the entries are deleted that duplicate rows that goes in a row.

Note: Only if you sort the internal table before all duplicates would be deleted.

0
Bruno Lucattelli On

This should work just fine.

REPORT z_teste_brl.

TYPES : ty_n TYPE n LENGTH 3.
DATA  : t_n TYPE TABLE OF ty_n WITH DEFAULT KEY.
DATA  : curr_n TYPE ty_n.
DATA  : last_n TYPE ty_n.

APPEND '101' TO t_n.
APPEND '101' TO t_n.
APPEND '102' TO t_n.
APPEND '101' TO t_n.
APPEND '102' TO t_n.
APPEND '103' TO t_n.
APPEND '103' TO t_n.

LOOP AT t_n INTO curr_n.
  IF sy-tabix GT 1 AND curr_n EQ last_n.
    DELETE t_n INDEX sy-tabix.
    CONTINUE.
  ELSE.
    last_n = curr_n.
  ENDIF.
ENDLOOP.

BREAK-POINT.
0
Sainath Rachaputi On

Suppose your table is: lt_tab. Then

LOOP AT lt_tab INTO ls_tab.
  IF ls_tab_prev EQ ls_tab.
    DELETE lt_tab INDEX sy-tabix.
  ENDIF.
  ls_tab_prev = ls_tab
ENDLOOP.

Another approach is : Suppose you have duplicate data in 'DUPCOL' In table lt_tab.

Then,

SORT lt_tab BY DUPCOL.
DELETE ADJACENT DUPLICATES FROM lt_tab COMPARING DUPCOL.