Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6690043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:35:26+00:00 2026-05-26T05:35:26+00:00

I have piece of code in C which removes any duplicate character in a

  • 0

I have piece of code in C which removes any duplicate character in a string .But i am doing it in two loops and would like it to optimize to one loop .

void removeDuplicates(char* ptr)
{
 int end = 1;
 int length = strlen(ptr);
 int current;
 int i;
 current = 1;
 for(end=1; end<length; end++)
 {
    for(i=0; i<end; i++)
    {
        if(ptr[end] == ptr[i])
        break;
    }
    if(i == end)
    {
      ptr[current] = ptr[i];
      current++;
    }
   }
   ptr[current] = 0;
 }

 int main(int argc, char** argv)
 {
  char str[256] = {0,};
  gets(str);
  removeDuplicates(str);
  printf("%s\n", str);
  return 1;
 }

How can i do this in one loop

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:35:27+00:00Added an answer on May 26, 2026 at 5:35 am

    You can do this in one pass with something like:

    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
        char str[] = "p123h12p97h62p32h";    // Input string.
        int found[256];                      // Assumes 8-bit char.
        char *src, *dst;                     // Source and destination pointers.
    
        // Output initial value and set found flags to false.
    
        printf ("Before: [%s]\n", str);
        memset (found, 0, sizeof(found));
    
        // One loop, processing each source character in string.
    
        for (src = dst = str; *src != '\0'; src++) {
            // If not yet found, flag it found and transfer it, else do nothing.
    
            if (!found[(unsigned int)(*src)]) {
                found[(unsigned int)(*src)] = 1;
                *dst++ = *src;
            }
        }
    
        // Insert new end of string, then output it.
    
        *dst = '\0';
        printf (" After: [%s]\n", str);
    
        return 0;
    }
    

    This removes duplicates in one single pass, using two pointers that advance independently through the string:

    src
     |
     v
     p123h12p97h62p32h
     ^
     |
    dst
    

    The src pointer advances by one each iteration of the loop. A character is copied from src to dst and the dst pointer is advanced only if the character has not been seen before (using the found array). The output is:

    Before: [p123h12p97h62p32h]
     After: [p123h976]
    

    Note the "assumes 8-bit char" comment – this is fine where you know the character set is ASCII (or any other 8-bit character set) but it may not be portable to more exotic implementations. You just have to ensure that there are enough elements in the found array for all of your characters. For example, a 10-bit char type would need 1024 elements (210 = 1024).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php loop through associative arrays i have this piece of code which
I have a piece of code which looks like this: Snippet A: class Creature
I would like to implement a shuffle function, I have this piece of code
I have a piece of code in an iPhone app, which removes all subviews
In JCIP we have a piece of code which looks like this: Listing 4.2:
I have this piece of code which check the last customer_id from the db.
I have this piece of code which I'm hoping will be able to tell
I have a piece of code which returns a Sales Order from AX. In
I am somewhat new to R, and i have this piece of code which
I have the following piece of code which outputs a long article: <?php the_content();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.