cancel
Showing results for 
Search instead for 
Did you mean: 

Duplicates in Last in internal table.

sri6666
Active Participant
0 Kudos
363

Hi Team, My internal table entries are like below

101

102

103

104

104

104

105

106

I want to sort the internal table like below

101

102

103

105

106

104

104

104

means repeated entries/duplicate (104) are should last in internal table.

Sandra_Rossi
Active Contributor
0 Kudos

What are the rules?

Just to move the lines with value 104 at the end?

If so, why is it difficult to read all these lines into another internal table LINES_104, delete them from the original table, and append LINES_104 to the end of the original table?

Accepted Solutions (1)

Accepted Solutions (1)

j_pavan_kumar
Product and Topic Expert
Product and Topic Expert

Hi Sri ,

Try the below piece of code.

it_tab = VALUE #( ( num = 101 )
( num = 102 )
( num = 103 )
( num = 104 )
( num = 105 )
( num = 104 )
( num = 104 )
( num = 105 ) ) .

SORT it_tab BY num.
DATA(it_final) = it_tab .
LOOP AT it_tab ASSIGNING FIELD-SYMBOL(<fs_tab>) GROUP BY ( num = <fs_tab>-num size = GROUP SIZE )
ASSIGNING FIELD-SYMBOL(<num_group>).
CHECK <num_group>-size > 1.
it_members = VALUE #( FOR wa_num_grp IN GROUP <num_group> ( wa_num_grp ) ).
APPEND LINES OF it_members TO it_dup .
DELETE it_final WHERE num = it_members[ 1 ]-num .
CLEAR: it_members.
ENDLOOP.

APPEND LINES OF it_dup TO it_final.

Answers (1)

Answers (1)

andrewfloriano
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Sri,

My first suggestion for your question would be:

SORT myinternaltable.
RETURN ADJACENT DUPLICATES FROM myinternaltable.

You can also go to SE30 and press F6. This will take you to the tips and tricks section. There, you will find some algorithms on how to efficiently deal with internal tables for loops and searching.

However, use two internal table is the best option. This definitely will not give any performance issue.

Kind Regards,

Andrew.