Board Logo
« Duplicate in the string »

Welcome Guest. Please Login or Register.
Jan 21st, 2018, 12:52am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Duplicate in the string  (Read 73 times)
Valentin
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 130
xx Duplicate in the string
« Thread started on: Nov 1st, 2017, 06:42am »

Hello all, please: how to remove duplicates in a string ?
The string: z$="aa bb cc dd ee ff bb dd aa ee gg"
Thank you.
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Duplicate in the string
« Reply #1 on: Nov 1st, 2017, 07:36am »

There more then one way to do it
And more then one way to define "duplicates"

Here's one

Code:
z$="aa bb cc dd ee ff bb dd aa ee gg"
print z$
'--------------------------------

prevC$=""
res$=""
for i = 1 to len(z$)
    c$=mid$(z$,i,1) 'current symbol
    if c$=prevC$ then   'compare with previous
        'skip all repeats
    else
        res$=res$+c$ 
        prevC$=c$       'reset  previous
    end if
next
'--------------------------------
print res$ 

 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Valentin
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 130
xx Re: Duplicate in the string
« Reply #2 on: Nov 1st, 2017, 07:42am »

I got there by modifying an old tsh73' algorithm
Code:


  a$="aa bb cc dd ee aa dd ff ee hh cc"
  print a$
  n=len(a$): dim a$(n)

for i = 0 to n 'take string in array$
    a$(i) = word$(a$,i+1)
next
for i = 0 to n-1
    for j = i + 1 to n
        if a$(i) = a$(j) then a$(j) = "*" 'mark duplicate with non value, use -1 here
    next
next
dim temp$(n)
index = 0
for i = 0 to n
    if a$(i) <> "*" then temp$(index) = a$(i) : index = index + 1
next
print "---------------------"
for i = 0 to n 'take array$ to string
   if a$(i)="*" then [pass]
    c$=c$+a$(i)+" "
    [pass]
next
    print c$
 

Old tsh73' algorithm it's véry good



« Last Edit: Nov 1st, 2017, 07:46am by Valentin » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls