Application Development and Automation 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: 
Read only

Find duplicate words in string.

Former Member
0 Likes
4,789

Hello.

For example, I have a string

my string = 'morning day evening day hello age day morning'

I need to write words that occurs more than 1 time. So for this example answer is:

morning, day

How can I solve this in abap?

Firstly I split string into table.

DATA itab TYPE TABLE OF string.
SPLIT my_string AT SPACE INTO itab.

So I have table itab with my words.

Now I need to select rows that dont repeat.

In SQL its easy to use

select * from itab groupby field_name having count(*) > 1

But how can I do that in ABAP?

10 REPLIES 10
Read only

DoanManhQuynh
Active Contributor
3,167

you can loop at group by and get the size of group, if it > 1 then it duplicate text.

Read only

Former Member
0 Likes
3,167

how to loop at group by at abap?

Read only

Read only

0 Likes
3,167

look at demo package in sap or simply F1 help on statement LOOP AT GROUP.

Read only

Muthu_raja
Active Participant
0 Likes
3,167
itab1[] = itab[].
Loop at itab assigning <fs_data>.
Read table itab1 assigning <fs_data1> with key table line eq <fs_data>.
if ( sy-subrc eq 0 ).
   write :/ <fs_data1>.
endif.
Endloop. 
Read only

matt
Active Contributor
0 Likes
3,167

You know that's the link to this question... Not massively helpful, I feel.

Read only

s1252
Product and Topic Expert
Product and Topic Expert
Read only

Former Member
0 Likes
3,167

Hi,

I prefer to use alternative internal table to store the count.

very simple is:

data MY_STRING type STRING value 'morning day evening day hello age day morning'.
data ITAB type table of STRING.

data: begin of ITAB2 occurs 0,
        WORDS type STRING,
        COUNT type INT4,
      end of ITAB2.

refresh ITAB.
split MY_STRING at SPACE into table ITAB.

"Counting duplicated words
refresh itab2.
loop at ITAB into data(LS_ITAB).
  clear itab2.
  ITAB2-WORDS = LS_ITAB.
  ITAB2-COUNT = 1.
  collect ITAB2.
endloop. 

"Read or loop with itab2 with condition
loop at ITAB2 where COUNT > 1.
  write: / ITAB2-WORDS, ITAB2-COUNT.
endloop. 


Regards,

Nam

Read only

matt
Active Contributor
3,167

For best efficency, ITAB2 should be a HASHED table.

Read only

0 Likes
3,167

Thanks Matthew for pointing it out.