"algorithm" two array string to Re-append

Source: Internet
Author: User

1 array de-weight

Python implementation

#调用内置函数去重def func (str): len1=len (str) len2=len (list (set (str))) Print ("the result of the de-weight is:", list (set (str)), "\ T de-weight number is:", ( Len1-len2)) #for cycle De-reset def func1 (str): nums=[]for N in str:if n not in nums:nums.append (n) print ("the result of the de-weight is:", sorted (nums), "\ T-de-weight number is: ", (len (str)-len (nums))) # while Go-to-reset def FUNC2 (str): Len1=len (str) for n in str:while str.count (n) >1:del str[ Str.index (n)]print ("the Result of the de-weight is:", sorted (str), "\ T de-weight number is:", (len1-len (str))) str=[1,3,2,4,2,4,1,6,4,5]func2 (str)

Dictionary to go Heavy

#调用内置函数去重def func (str): len1=len (str.values ()) len2=len (list (set (str.values ())) Print ("the result of the de-weight is:", list (set ( Str.values ())), "\ t de-weight number is:", (len1-len2)) #for loop to reset def func1 (str): nums=[]for N in str.values (): if n not in Nums:nums.append (n) print ("the result of the de-weight is:", sorted (nums), "\ T de-weight number is:", (len (str.values ())-len (nums))) str=[1,3,2,4,2,4,1,6,4,5]dirc={ 1:1,2:2,3:3,4:3}FUNC2 (dirc)

String Append to Redo sort

#调用内置函数去重def func (str1,str2):p rint ("the result of the de-weight is:", sorted (set (str1+str2)), "\ T to remove the number of weight is:", (len (str1+str2)-len (set (str1+ Str2)))) #for cyclic de-weight def func1 (str1,str2): nums=[]str=str1+str2for N in str:if n No in nums:nums.append (n) print ("the result of the De-weight is:", Sorted (nums), "\ t go to weight number is:", (len (str)-len (nums))) # while go to heavy def Func2 (str1,str2): str=str1+str2for N in Str:while str.count (n) >1:del str[str.index (n)]print ("the result of the de-weight is:", sorted (str), "\ T de-weight number is:", (len (str1+str2)-len (str))) str1=[' very good ', ' good ' , ' Good ', ' Very ', ' book ', ' I ', ' love ', ' I ']str2=[' Java ', ' C # ', ' Python ', ' C # ']func2 (str1,str2)

Java implementation

Import Java.util.linkedlist;import Java.util.list;public class func {public static void unique (string[] Str) {//array_ Unique      list<string> List = new linkedlist<string> ();      for (int i = 0; i < str.length; i++) {         if (!list.contains (str[i])) {              list.add (string.valueof (str[i]));          }      }      System.out.print ("after The weight of the results:" +list+ "\ T total number:" + (str.length-list.size ()));} public static void main (string[] Args) {string[] str={"1", "3", "2", "4", "2", "4", "1", "6", "4", "5"};unique (str);}

Set implementation

Import Java.util.hashset;import Java.util.set;public class func {public static void unique (string[] Str) {set<string& Gt set = new Hashset<string> ();        For (int i=0; i<str.length; i++) {            set.add (str[i]);        }     System.out.print ("after The weight of the results:" +set+ "\ T total number:" + (str.length-set.size ()));} public static void main (string[] Args) {string[] str={"python", "SQL", "c #", "Java", "c", "python", "R", "Matlab", "c + +", " SQL "};unique (str);}}

"algorithm" two array string to Re-append

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.