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: 

Find duplicate words in string.

Former Member
0 Kudos

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

DoanManhQuynh
Active Contributor

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

Former Member
0 Kudos

how to loop at group by at abap?

0 Kudos

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

Muthuraja
Active Participant
0 Kudos
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. 

matt
Active Contributor
0 Kudos

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

s1252
Participant

0 Kudos

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

For best efficency, ITAB2 should be a HASHED table.

0 Kudos

Thanks Matthew for pointing it out.