JavaScript string deletion of repeating characters _javascript tips

Source: Internet
Author: User
Tags hash

This section describes how to delete a string of repeated characters, regardless of the actual value, as a learning algorithm is also very good.

The code is as follows:

function Droprepeat (str) {
 var result=[];
 var hash={};
 For (var i=0, elem; i<str.length;i++) {
  elem=str[i];
  if (!hash[elem]) {
   hash[elem]=true;
   Result=result+elem
  }
 }
 return result;
}

The functions in the above code can remove repeated characters from a string, using the example:

 

The return value is: ABCD.

Here's to share python: get rid of repeated characters in a string

Python 2.7:
#-*-encoding:utf-8-*-
string = ' Abc123456ab2s '
r = '. Join (x for I, X in Enumerate (string) if String.index (x) = = i)
print string
print R

The output is as follows:

Abc123456ab2s
abc123456s

Related Article

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.