Delete repeated array elements (js, java, c #)

Source: Internet
Author: User


Compile a method to efficiently remove repeated elements from the array.
Copy the Code as follows:
<Script>
Function unique (data ){
Data = data | [];
Var a = {};
For (var I = 0; I <data. length; I ++ ){
Var v = data [I];
If (typeof (a [v]) = 'undefined '){
A [v] = 1;
}
};
Data. length = 0;
For (var I in ){
Data [data. length] = I;
}
Return data;
}
Function test (){
Var arr = [9, 1, 3, 8, 7, 6, 6, 5, 7, 8, 7, 4, 3, 1];
Var arr1 = unique (arr );
Alert (arr1.join (","));
}
Test ();
</Script>

Output result:
9, 1, 3, 8, 7, 6, 5, 4
Deduplication of the js array removes the repeated elements in the array:
Copy the Code as follows:
Array. prototype. delrepeat = function (){
Var newarray = new array ();
Var len = this. length;
For (var I = 0; I <len; I ++ ){
For (var j = I + 1; j <len; j ++ ){
If (this [I] === this [j]) {
J = ++ I;
}
}
Newarray. push (this [I]);
}
Return newarray;
}

However, it is obvious that the for loop is embedded with another for loop. It must be time-consuming for a large amount of data! Inefficient! A new method has been optimized through search and expert guidance:
Copy the Code as follows:
Array. prototype. delrepeat = function (){
Var newarray = [];
Var provisionaltable = {};
For (var I = 0, item; (item = this [I])! = Null; I ++ ){
If (! Provisionaltable [item]) {
Newarray. push (item );
Provisionaltable [item] = true;
}
}
Return newarray;
}

A temporary provisionaltable object is used to take the value of the array as the key value of the provisionaltable object. If the corresponding value does not exist, the value of this array is pushed to the new array.


Java

Public static void main (string [] args ){
Getdistinct (new int [] {6, 7, 3, 6, 5, 2, 7, 8 });
}

Static void getdistinct (int array []) {
Java. util. list = new java. util. arraylist ();
For (int I = 0; I <array. length; I ++ ){
If (! List. contains (array [I]) {
List. add (array [I]);
System. out. print (array [I] + "");
}
}
}
Output: 6 7 3 5 2 8

Array

Public boolean contains (object elem ){
Return indexof (elem)> = 0;
}

Public int indexof (object elem ){
If (elem = null ){
For (int I = 0; I <size; I ++)
If (elementdata [I] = null)
Return I;
} Else {
For (int I = 0; I <size; I ++)
If (elem. equals (elementdata [I])
Return I;
}
Return-1;
}

Use c # To find duplicate data in the data and delete the array repeated items. Personally, if the array is not very large, it is still the fastest to replace it with regular expressions. The principle is to sort and repeat with regular expressions. Of course, generic + foreach is the best choice. The second method is very good, and the code is concise. It is only a generic application. It would be better if foreach is used.

Method 1:

Public static string [] removedup (string [] mydata) {if (mydata. length> 0) {array. sort (mydata); // sorts the array first int size = 1; // defines the length of the array after the repeated items are deleted for (int I = 1; I <mydata. length; I ++) if (mydata! = Mydata [I-1]) size ++; string [] mytempdata = new string [size]; int j = 0; mytempdata [j ++] = mydata [0]; for (int I = 1; I <mydata. length; I ++) // traverses the array member if (mydata! = Mydata [I-1]) // If two adjacent values are not equal, add the new array mytempdata [j ++] = mydata; return mytempdata;} return mydata ;}

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.