Duplicate characters in C # filter string

Source: Internet
Author: User
Tags int size

These days to do a project, just want to use to remove the string repetition of the characters, began to do not know how to write this algorithm, but through the Internet to find some relevant information, finally there is a general idea, so set thinking broad discussion bar, summed up the following two kinds:

This is passed in an array:

public static string[] Removedup (string[] myData)
{

if (Mydata.length > 0)
{
Array.Sort (MyData)//using array sorting, personal feeling can improve a little more efficiency when the elements of the array are many!

int size = 1; At least 1
for (int i = 1; i < mydata.length; i++)
if (Mydata[i]!= mydata[i-1])
size++;

string[] Mytempdata = new String[size];

int j = 0;

Mytempdata[j++] = mydata[0];

for (int i = 1; i < mydata.length; i++)
if (Mydata[i]!= mydata[i-1])
Mytempdata[j++] = Mydata[i];

return mytempdata;
}

return myData;
}

This is passed in a string that is no different than the one above.
public static string[] Removedupstring (String myString)
{
string[] MyData = Mystring.split (' # ');
if (Mydata.length > 0)
{
Array.Sort (MyData);

int size = 1; At least 1
for (int i = 1; i < mydata.length; i++)
if (Mydata[i]!= mydata[i-1])
size++;

string[] Mytempdata = new String[size];

int j = 0;

Mytempdata[j++] = mydata[0];

for (int i = 1; i < mydata.length; i++)
if (Mydata[i]!= mydata[i-1])
Mytempdata[j++] = Mydata[i];

return mytempdata;
}

return myData;
}

Write a little bit of algorithm, I think my ability will improve faster! This is what I learned from my colleagues!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.