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

  • Home
  • SEARCH
  • 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 711995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:47:55+00:00 2026-05-14T04:47:55+00:00

The following code is trying to remove any duplicate characters in a string. I’m

  • 0

The following code is trying to remove any duplicate characters in a string. I’m not sure if the code is right. Can anybody help me work with the code (i.e whats actually happening when there is a match in characters)?

public static void removeDuplicates(char[] str) {
  if (str == null) return;
  int len = str.length;
  if (len < 2) return;
  int tail = 1;
  for (int i = 1; i < len; ++i) {
    int j;
    for (j = 0; j < tail; ++j) {
      if (str[i] == str[j]) break;
    }
    if (j == tail) {
      str[tail] = str[i];
      ++tail;
    }
  }
  str[tail] = 0;
}
  • 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-14T04:47:55+00:00Added an answer on May 14, 2026 at 4:47 am

    The function looks fine to me. I’ve written inline comments. Hope it helps:

    // function takes a char array as input.
    // modifies it to remove duplicates and adds a 0 to mark the end
    // of the unique chars in the array.
    public static void removeDuplicates(char[] str) {
      if (str == null) return; // if the array does not exist..nothing to do return.
      int len = str.length; // get the array length.
      if (len < 2) return; // if its less than 2..can't have duplicates..return.
      int tail = 1; // number of unique char in the array.
      // start at 2nd char and go till the end of the array.
      for (int i = 1; i < len; ++i) { 
        int j;
        // for every char in outer loop check if that char is already seen.
        // char in [0,tail) are all unique.
        for (j = 0; j < tail; ++j) {
          if (str[i] == str[j]) break; // break if we find duplicate.
        }
        // if j reachs tail..we did not break, which implies this char at pos i
        // is not a duplicate. So we need to add it our "unique char list"
        // we add it to the end, that is at pos tail.
        if (j == tail) {
          str[tail] = str[i]; // add
          ++tail; // increment tail...[0,tail) is still "unique char list"
        }
      }
      str[tail] = 0; // add a 0 at the end to mark the end of the unique char.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying the following code to execute a search and it's not working. On
I'm trying to remove diacritic characters from a pangram in Polish. I'm using code
I am trying the following code in eclipse: public class A { List<Integer> intList
I am trying the following code to AutoComplete contact details as the user types
I'm trying the following code http://code.google.com/apis/ajax/playground/#change_the_playing_video It works well when runned from the playground
I am trying to run the following code that I found marked as correct
I'm trying to execute the following code, but I'm getting SyntaxError: invalid syntax at
I am trying to convert the following code from Trefethen's Spectral Methods in MATLAB
I'm trying to get the following code to work using both Watin and HttpWatch.
I am trying to run the following code from within Eclipse: Process process =

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.