Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Delete Duplicates without Sorting

Former Member
0 Kudos

Hi Gurus,

I need to delete duplicates from my ITAB and that too without sorting .

Is there any way I can do it ??

Will appreciate a faster response.

Thanks,

Chandan

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Try this one.For example it_tab is the table you are having. Now create a dummy int tab it_dmy same as it_tab.

it_dmy = it_tab.

sort it_dmy by field1 ... .

delete adjacent duplicates from it_dmy comparing field1 ... .

loop at it_tab into wa_tab.
w_index = sy-tabix.
read table it_dmy with key field1 = wa_tab-field1 transporting no fields.
if sy-subrc <> 0. "If the entr not exists in dummy table then delete it from original table
delete it_tab index w_index.
endif.
endloop.

Hope this helps you.

Regards,

Manoj Kumar P

Edited by: Manoj Kumar on May 25, 2009 6:14 AM

12 REPLIES 12

Former Member
0 Kudos

Hi Chandan,

U can use clear statement..Y u don't want to sort itab?

Edited by: Karthik R on May 25, 2009 9:13 AM

0 Kudos

I cant do SORT because my requirement is like that, I can't afford to change the Order of entries.

Can you please tell me the Syntax for CLEAR statement in order to remove the duplicates ?

Normal Clear I know.

Former Member
0 Kudos

Hello,

If you are not doing sorting it will not delete all the records but if you sort it the data output will be not according to u r requirements. so keep the loop to the internal table and delete each and every record.

Former Member
0 Kudos

Hi Chandan,

Before appending the table ..just write the read statement on the same internal table to check whether the record is available with the same key or not. IF it is found dont append ..thats it.

Regards,

Kumar Bandanadham

Peranandam
Contributor
0 Kudos

Below logic will resolve your problem

&----


*& Report ZDUPLICATE

*&

&----


*&

*&

&----


REPORT ZDUPLICATE.

TYPES: BEGIN OF ty,

a TYPE C,

b TYPE C,

END OF ty.

DATA: itab TYPE TABLE OF ty,

wa LIKE LINE OF itab,

wa1 LIKE LINE OF itab.

data: v_tabix like sy-tabix.

wa-a = 'r'.

wa-b = 's'.

APPEND wa TO itab.

wa-a = 'g'.

wa-b = 'l'.

APPEND wa TO itab.

wa-a = 'r'.

wa-b = 's'.

APPEND wa TO itab.

wa-a = 'r'.

wa-b = 's'.

APPEND wa TO itab.

wa-a = 'r'.

wa-b = 's'.

APPEND wa TO itab.

LOOP AT itab INTO wa.

v_tabix = sy-tabix.

add 1 to v_tabix .

LOOP at itab FROM v_tabix INTO wa1 where a = wa-a.

DELETE itab INDEX sy-tabix.

endloop.

ENDLOOP.

Regards,

Peranandam

Former Member
0 Kudos

Hi,

Try this one.For example it_tab is the table you are having. Now create a dummy int tab it_dmy same as it_tab.

it_dmy = it_tab.

sort it_dmy by field1 ... .

delete adjacent duplicates from it_dmy comparing field1 ... .

loop at it_tab into wa_tab.
w_index = sy-tabix.
read table it_dmy with key field1 = wa_tab-field1 transporting no fields.
if sy-subrc <> 0. "If the entr not exists in dummy table then delete it from original table
delete it_tab index w_index.
endif.
endloop.

Hope this helps you.

Regards,

Manoj Kumar P

Edited by: Manoj Kumar on May 25, 2009 6:14 AM

awin_prabhu
Active Contributor
0 Kudos

Hi chandan,

Try like below.

REPORT ztest.

DATA: BEGIN OF itab OCCURS 0,

name TYPE string,

num TYPE i,

END OF itab,

itab1 LIKE itab OCCURS 0 WITH HEADER LINE,

wa LIKE itab,

wa1 LIKE itab,

f TYPE i VALUE 1,

count TYPE i VALUE 0,

f1 TYPE i.

wa-name = 'ram1'.

wa-num = '1'.

APPEND wa TO itab.

wa-name = 'ram2'.

wa-num = '2'.

APPEND wa TO itab.

wa-name = 'ram3'.

wa-num = '3'.

APPEND wa TO itab.

wa-name = 'ram4'.

wa-num = '4'.

APPEND wa TO itab.

wa-name = 'ram5'.

wa-num = '5'.

APPEND wa TO itab.

wa-name = 'ram2'.

wa-num = '6'.

APPEND wa TO itab.

itab1[] = itab[].

CLEAR: wa, wa1.

LOOP AT itab INTO wa.

LOOP AT itab1 WHERE name = wa-name.

IF f NE 1.

count = count + 1.

IF count > 1.

f1 = f - 1.

DELETE itab INDEX f1.

ENDIF.

ENDIF.

f = f + 1.

ENDLOOP.

CLEAR count.

ENDLOOP.

Former Member
0 Kudos

DELETE ADJACENT DUPLICATES FROM itab.

Former Member
0 Kudos

You can directly use :

Delete adjacent duplicates from ITAB comparing your fields. But this will not ensure all the duplicates will be deleted.

Please ellaborate on the requirement. it seems to me that you have scenario like

F1 F2

A 2

A 1

A 6

You need to retain the first entry what so ever is the sequence (i.e. A 2). You need to delete the entries A 1 and A 6. But if you sort it it will change the sequence to A 1, A 2 and A 6.

If yes, then use "stable sort"

sort itab by f1 f2 stable.
delete adjacent duplicates from itab comparing f1.

it will retain your sequence.

Last ioption is move the contants of ITAB[] to temporary table TEMP[]. Sort temp and delete adjacent duplicates. After that loop at ITAB and compare the results of two table.

Hope this will help.

Former Member
0 Kudos

Hi,

Try this.

If u dont want to sort the ITAB, use a local internal table L_ITAB. Sort this table and delete the adjacent duplicates. Then check the entries in the ITAB with L_ITAB and delete the values from the ITAB.

1. Move the data from ITAB to L_ITAB.

2. Sort L_ITAB and delete adjacent duplicates from it.

3. Loop the ITAB.

Read the L_ITAB

if sy-subrc ne 0.

delete ITAB.

endif.

Hope this may be helpful.

Regards,

Sharin.

Former Member
0 Kudos

Hi,

use this code for sure your issue will be resovled....

data wa1 like line of itab.
data w_index type sy-index.
LOOP AT itab INTO wa.
w_index = sy-tabix.
LOOP AT itab into wa1 FROM w_index WHERE field1 = wa-field1.
"  in the where clause you can give more than one field condition depending on the uniqueness of the record
" due to which there are duplicates....
count = count + 1.
IF count > 1.
DELETE itab.
ENDIF.
ENDLOOP.

Regards,

Siddarth

Former Member
0 Kudos

hi,

Just add one field to itab.

update the filed by tabix .

then sort the table as per the key and delete adjacent duplicates.

and again sort as per the new field, so that your sequence will remain as it.

Thanks & Regards,

Kimaya