In js, how does one remove repeated elements in a specified array? I opened the firebug console and spent 30 seconds thinking, writing 30 seconds. The result is as follows: (x is the target array, and y is the result after repeated elements are removed)
The Code is as follows:
Var x = [1, 1, 3, 4, 5, 3];
Var y = [];
Var tArray = function (I, arr ){
Var yap = false;
For (var j = 0; jif (arr [j] = I) {yap = true; break ;};
}
If (! Yap) arr. push (I );
};
For (var t = 0; t TArray (x [t], y );
}
Alert (y. length );
Alert (y. toString ());
The answer is not unique and there is no standard answer, so the above Code is correct and wrong. What if the array is a complex object array? What if the array contains multiple null objects? You need to know that js contains many special and even bug phenomena. alert ({}={}) to see what is going on...
Note: if anyone has a standard answer, please post it in the comments for everyone to learn.
The problem will not be taken into account by the author.
Do not study and do questions. The ability to efficiently solve problems in actual work is truly valuable.
Note the following:
Pay attention to the following issues:
Arr [j] = I you seem to be referring to the Equality judgment when the array element is of the number type...
However, we may need to make equality judgments for different types of applications.
1! = New Number (1)
Null = undefined
0 = "0" and so on
Therefore, the simple = and = are not suitable.
An equals method should be implemented independently for Equality judgment... rules should be set as needed
The example is for the number array to express an attitude towards "Interview Questions. The actual application should be considered based on actual needs. Unless required in actual work or I am a js academic researcher, I will not implement an equals method independently. Developers outside China often follow The principle: "Do The Simplest Thing That cocould Possibly Work", which means "Do not over-design, apply first ", my work experience over the past few years often proves this truth. I don't know what you think.