Array deduplication
Src: http://yiminghe.javaeye.com/blog/524716
This is a classic problem. After removing the repeated elements in the array, I searched the internet and found that most of them are the solution regardless of reposted or original:
Solution:
Js Code
- Function uniq (array ){
- Var map = {};
- Var re = [];
- For (var I = 0, l = array. length; I <l; I ++ ){
- If (typeof map [array [I] = "undefined "){
- Map [array [I] = 1;
- Re. push (array [I]);
- }
- }
- Return re;
- }
function uniq(array) {var map={};var re=[];for(var i=0,l=array.length;i<l;i++) {if(typeof map[array[i]] == "undefined"){map[array[i]]=1;re.push(array[i]);}}return re;}
If you have used advanced languages such as java, there is no problem with this code: the following example:
Js Code
- Uniq ([1, 2, 2, 4]);
uniq([1,2,1,2,4]);
It can also run normally.
Try the following example again:
Js Code
- Uniq ([{x: 1}, "[object Object]"]);
- Uniq ([{x: 1 },{ z: 2}]);
uniq([{x:1},"[object Object]"]);uniq([{x:1},{z:2}]);
You will know where the error is!
HashMap In Java:
In java, the HashMap class can use objects as keys (internal implementation uses hashcode to hash to the bucket and equals in the bucket [Default memory address] comparison), such:
Java code
- Class Holder {
- Int I;
- }
- Public class Test3 {
- /**
- * @ Param args
- */
- Public static void main (String [] args ){
- HashMap <Holder, Holder> map = new HashMap <Holder, Holder> ();
- Holder key = new Holder ();
- Holder value = new Holder ();
- Value. I = 1;
- Map. put (key, value );
- Holder key2 = new Holder ();
- System. out. println (map. get (key). I );
- System. out. println (map. get (key2 ));
- }
- }
class Holder {int i;}public class Test3 {/** * @param args */public static void main(String[] args) {HashMap<Holder, Holder> map=new HashMap<Holder, Holder>();Holder key= new Holder();Holder value= new Holder();value.i=1;map.put(key, value);Holder key2=new Holder(); System.out.println(map.get(key).i);System.out.println(map.get(key2));}}
Object In Javascript:
In javascript, after all, there is no map. Only objects are defined. Objects Require that their attribute values be strings. If the attribute provided to objects is not strings, the toString method will be automatically called to convert it to the string format, for example:
Js Code
- Var x = {};
- Var y = {
- ToString: function (){
- Return "z ";
- }
- };
- X [y] = 1;
- Alert (x ["z"]);
var x={};var y={ toString:function(){ return "z"; }};x[y]=1;alert(x["z"]);
The above example shows why the first program is wrong.
Correct answer:
We cannot use the map class library provided by the advanced language, so we have to traverse the array twice. It is also the standard answer provided by taobao ued:
Note === use.
Js Code
- /**
- * Unique the array
- * @ Param {Array} array to unique
- * @ Return {Array} uniqued array, note change parameter
- */
- Function undulpicate (array ){
- For (var I = 0; I <array. length; I ++ ){
- For (var j = I + 1; j <array. length; j ++ ){
- // Note ====
- If (array [I] === array [j]) {
- Array. splice (j, 1 );
- J --;
- }
- }
- }
- Return array;
- }
/*** Unique the array * @ param {Array} array Array to unique * @ return {array} uniqued array, note change parameter */function undulpicate (array) {for (var I = 0; I <array. length; I ++) {for (var j = I + 1; j <array. length; j ++) {// note === if (array [I] === array [j]) {array. splice (j, 1); j -- ;}} return array ;}
Ps: Jquery Uniq Node
If we confirm that each element in the array is an object, we can add tags to the object elements to increase the time complexity to O (n ):
Js Code
- Var x = {z: 1 };
- Var y = {q: 2 };
- Function uniqObjects (array ){
- Var re = [];
- For (var I = 0, l = array. length; I <l; I ++ ){
- If (typeof array [I] ["_ uniqObjects"] = "undefined "){
- // Add tags
- Array [I] ["_ uniqObjects"] = 1;
- Re. push (array [I]);
- }
- }
- // Retrieve the tag
- For (var I = 0, l = re. length; I <l; I ++ ){
- Delete re [I] ["_ uniqObjects"];
- }
- Return re;
- }
- UniqObjects ([x, y, x]);
Var x = {z: 1}; var y = {q: 2}; function uniqObjects (array) {var re = []; for (var I = 0, l = array. length; I <l; I ++) {if (typeof array [I] ["_ uniqObjects"] = "undefined ") {// Add the tag array [I] ["_ uniqObjects"] = 1; re. push (array [I]) ;}/// retrieves the tag for (var I = 0, l = re. length; I <l; I ++) {delete re [I] ["_ uniqObjects"];} return re;} uniqObjects ([x, y, x]);
This is exactly the idea of jquery. Since each element is a node array, you can do this:
Js Code
- Unique: function (array ){
- Var ret = [], done = {};
- Try {
- For (var I = 0, length = array. length; I <length; I ++ ){
- Var id = jQuery. data (array [I]);
- If (! Done [id]) {
- Done [id] = true;
- Ret. push (array [I]);
- }
- }
- } Catch (e ){
- Ret = array;
- }
- Return ret;
- },
unique: function( array ) {var ret = [], done = {};try {for ( var i = 0, length = array.length; i < length; i++ ) {var id = jQuery.data( array[ i ] );if ( !done[ id ] ) {done[ id ] = true;ret.push( array[ i ] );}}} catch( e ) {ret = array;}return ret;},
Note: Do not use this method for basic types, such as number and string. They will generate temporary objects and cannot achieve the expected results!