开发者

Remove duplicates from String

开发者 https://www.devze.com 2023-03-28 15:14 出处:网络
Here is my code, public static String set_x_dates() { int noRecords = GlobalData.getNoRecords(); int n; String date = \"\";

Here is my code,

   public static String set_x_dates()
   {
    int noRecords = GlobalData.getNoRecords();
    int n;
    String date = "";
    if (noRecords <= 10)
        for (n = 0; n < noRecords; n+开发者_开发知识库+)
            date += Dates[n] + "-" + Month[n] + "|";
    else {
        for (n = 0; n < noRecords; n++) {
            int gap = (int) (noRecords / 10);
            date += Dates[n] + "-" + Month[n] + "|";
            n++;
            if (n != noRecords)
                for (; gap > 0; gap--)
                    date += "|";
        }

    }
    return date;
}

I want to remove duplicate entries from the string "date" which is being return. Dates[] and Month[] are static int arrays. Can anybody help me?

The output I'm getting is this:

25-5|28-5|4-6|8-6|10-6|14-6|17-6|7-7|7-7|7-7|7-7|7-7|7-7|7-7|7-7|7-7|7-7|26-7|26-7|

and I want this:

25-5|28-5|4-6|8-6|10-6|14-6|17-6|7-7|26-7| 


Instead of concatenating dates to a string, add your dates to a Set as you loop over the records. Sets cannot contain duplicates.

Then at the end of the method, loop over your set and construct a string to return.


You could assemble a Set of strings that will be concatenated after the set is populated.

Edit: ah, dogbane got there first :P


Below is the code for removing duplicates in String.

Remove duplicates from String

Output :

Remove duplicates from String

0

精彩评论

暂无评论...
验证码 换一张
取 消