js string to achieve a weight to weight and the array

https://blog.csdn.net/charles_tian/article/details/79938010

Title: implement a function to remove repeated characters in the input string.

Title: implement a function, the input array remove duplicate elements.

I think this question is that we often met it, is not as if every time they encounter a change will not? That is because the lack of concentration, had time to learn the knowledge Chuqian looked at the answers will no longer focus on the idea, and this is certainly not, in order to go further on the road programmers have to understand that the solution of this question principles and practices by which methods you do not know, but also to compare which method is even better, understand better where the principle of this method comes from, so as to further and further away.

For such problems, the Internet has a lot of answers, I'm just here to be a summary, most part belong to reprint, but I hope you insist on reading, because you want to focus on understanding and logical thinking, only right to help.

1) to re-string

1.for traversal

The main idea: First, a new empty string, then declare a condition judgment flag as below, and then began to enter the string for loop, a for loop in the flag value of 1, then in a for loop and a for loop, circulating inside the loop where the new character string, and then determination is given, if the same, flag = 0, and out of the loop, the final judgment of the flag, if 1, not representing the character appeared in the cycle where the new string , then add the characters to a new string; If 0, no processing, skip, and returns the value of the new string.

This method is not recommended, because the implementation of the two traverse, more consumption of performance, we can also learn test data, this method is relatively time-consuming.

Code is implemented as follows:

function removeRepeatStr(str){
var newStr = '';
var flag;
var len = str.length;
for(var i=0; i<len; i++){
flag = 1;
var newLen = newStr.length;
for(var j=0; j<newLen; j++){
if(str[i] == newStr[j]){
flag = 0;
break;
}
}
if(flag){
newStr = newStr + str[i];
}
}
return newStr;
}
2)indexOf方法(无兼容问题)

The main idea: To create is an empty string, then the string form of traversal of traversal input, and then determine whether traversal character is the first time in the new string, and if so, added to an empty character strings; if not, skip, -1. Finally, return the new string.

In essence, this method is not much difference with the first, but became the judge of whether the conditions for the first time, and because this method is essentially indexOf also find a character in the character string in the form of traversal the position of the first occurrence of the string, if the string is not found, returns -1. Therefore, this method is not recommended too, because consumption is relatively performance. Find character as a direct index value to the quick.

Code is implemented as follows:

function removeRepeatStr(str){
var newStr = '';
var len = str.length;
for(var i=0; i<len; i++){
if(newStr.indexOf(str[i])==-1){
newStr = newStr + str[i];
}
}
return newStr;
}
3)search方法

The main idea: in line with the second method, only a change of a detection method. IndexOf method is about to become a search method, we know that indexOf method is to traverse a string find the location of a character in the string of the first occurrence, if not found in the string, or -1; and search such method is, for substring specified search character string, or retrieve regular expression matching substring. If no matches any substring, or -1.

The second method is relatively speaking, a little better, but this is only a little bit.

Code is implemented as follows:

function removeRepeatStr(str){
var newStr = '';
var len = str.length;
for(var i=0; i<len; i++){
if(newStr.search(str[i])==-1){
newStr = newStr + str[i];
}
}
return newStr;
}
4)对象属性

The main idea: to create a new null object and an empty string of input traversal, the traversal of the character object and as a property value of 1, then the attribute value is determined by the object whether to repeat array elements (i.e., every traversal of arr element should be noted here that, at this time did not put into the element to the object, which is determined as the first object properties whether the current object attribute already exists), if not, add to the new string; if so, skip. Finally, return the new string.

To better understand this, I will focus on two places where interpretation is also relatively difficult to understand.

First: judgment code, if (! Obj [str [i]]) {}, I have just explained, this condition is determined the value of object properties, str [i] is an object property, if the value of object properties exist, it illustrates the property already exists in an object; there is no value, then deposited into the current character string;

Second: the code assignment, obj [str [i]], the object is to ensure this property has a value, if not the assignment, the default value is undefined object property;

Next is the joint understanding: When traversing the first character, and the object is initially empty, so when the first character as an object attribute, it is of no value, no value, then it defaults to undefined, ie obj [ str [0]] == undefined, is determined in the statement, is undefined converted to false, if converted into digital type, or 0; i.e. the determination statement obj [str [0]] == fasle, if so (! obj [str [0]]) == if (true), it will execute the following statement, which is deposited into the first character string, and to obj [str [0]] assignment, if you do not assignment, equivalent to or undefined, wait until the traverse to duplicate elements, do not skip, or will be deposited into a string, which would not achieve deduplication effects. If the assignment, obj [str [0]] = 1, it is determined that the statement will be converted to the true value (here do not understand why turn true, then you have to go back and read a book), and there in front of a non No, it is judged that the statement of the result is false, it will not execute the following statement, and to achieve a skip operation, also reached deduplication results.

This method is better than the above-mentioned several methods, after all, to find a property value in the object inside, just to match the property name on it, not all of the traverse, to save time.

Code is implemented as follows:

removeRepeatStr function (STR) {
var obj = {};
var newStr = '';
var len = str.length;
for (var I = 0; I <len; I ++) {
IF (! obj [STR [I]]) {
newStr newStr + STR = [I];
obj [STR [I]] = 1; // Note that the object here is to give a property assignment, this value can be arbitrarily taken. It means to traverse each character stored as an object property, and assign, to ensure the uniqueness of the attribute
}
}
return newStr;
}
2) array deduplication

1.indexOf method

The main idea: to go with the above string of similar weight. Create a new empty array, then traverse the array passed, if the new array does not traverse the elements, then added to the new array; if so, skip. Finally, return the new array that is free of duplicate array elements.

Before analysis with similar strings, this method is also more time-consuming and a waste of resources.

Code is implemented as follows:

function removeRepeatArrElement(arr){
var newArr = [];
var len = arr.length;
for(var i=0; i<len; i++){
if(newArr.indexOf(arr[i])==-1){
newArr.push(arr[i]);
}
}
return newArr;
}
2.对象属性

The main idea: go heavy with strings similar, not repeat them here. Anyway, better than double for the cycle and indexOf methods.

Code is implemented as follows:

removeRepeatArrElement function (ARR) {
var obj = {};
var newArr = [];
var len = arr.length;
for (var I = 0; I <len; I ++) {
IF (obj [ARR [I]]!) here {// attributes can also be determined whether there is an object, i.e., iF (obj.hasOwnProperty (ARR [I])!)
newArr.push (ARR [I]);
obj [ARR [I]] = to true;
}
}
return newArr;
}
3.for cycle

The main idea: that is, an array of individually moved to another array, when faced with repeating elements, does not move; if not repeat the move to the new array.

This method is not recommended, because it uses a double for the cycle, a large performance overhead, because when traversing i, and j in i inside traversal, such as i and j from each of 10, then the total number of traverse of 100 times, that is, when i = 0 when, j 1,2,3,4,5,6,7,8,9,10 were taken, and when i = 1, j which went 10 values ​​were taken until i = 10 , j = 10 until the end, and the performance is quite time consuming. And here is the first time a matching element to find whether the element behind its repeated, i.e. when i = 0 when, j take 1-9; when i = 1, j taking 2-9 ...; when when i = 8, j taking 9; when i = 9 when, j takes less value is undefined, so the determination is false, the direct output of the first index value 9 (the tenth element) of the element.

Code is implemented as follows:

function removeRepeatArrElement(arr){
var newArr = [];
var len = arr.length;
var flag;
for(var i=0; i<len; i++){
flag = true;
for(var j=i; j<len-1; j++){
if(arr[i] == arr[j+1]){
flag = false;
break;
}
}
if(flag){
newArr.push(arr[i]);
}
}
return newArr;
}
 

4.value -> key method

The main idea: As the name suggests, is to assign a value to an array of another array of keys, then repeated elements are removed, and then take the keys of the new array as a result of de-duplicated

Code is implemented as follows:

removeRepeatArrElement function (ARR) {
var newArr = [];
var TEMP = [];
for (var I in TEMP) {
TEMP [ARR [I]] =. 1;
}
for (var I in TEMP) {
newArr.push (I );
}
return newArr;
}
However, this method thing to note is that the elements of the output array is given in form of a string, instead of the number type or other type. So in order to get back the type of data you want, it will further post-processing.

 

5.sort () method

The main ideas: First, sort the array, and then sort we all know, the same elements will be together, and then the cycle, followed by comparing two adjacent elements are the same, if the same is skipped; if different, the addition of to the new array.

Code is implemented as follows:

removeRepeatArrElement function (ARR) {
var newArray arr.sort = ();
var newArr = [];
var len = newArray.length;
for (var I = 0; I <len; I ++) {
! IF (newArray [I] = newArray [I +. 1]) {
newArr.push ([newArray [I]]);
}
}
}
return newArr;
}
the console.log (removeRepeatArrElement ([12,4,3,5,6,23,4,5, 6])); // [12, 23, 3, 4, 5, 6]
following line I test, we can see that duplicate numbers have been removed, and then outputs [12, 23, 3, 4, 5, 6].

But here I had a doubt: sort () method brackets without default function is ascending output (output in accordance with a character encoding order), so now the output is [12, 23, 3, 4, 5, 6] instead of [3, 4, 5, 6, 12, 23].

If interested students can learn about other native j s sort implementation code, you can refer to the following links: js and js native sorting algorithm sorting algorithm.

The last point is also mentioned, do not know if students have a question, why the de-emphasis method, the time value of object attributes object property method to determine, when an object is used to take the property in square brackets, rather than a point method?

Because the dot syntax and bracketing is still a difference:

1, the bracketing may be used as a variable name attribute, while not point method;

2, the bracketing may be used as a numeric attribute name, not the dot syntax;

3, may be used in the bracketing js keywords and reserved words as the attribute name, not the dot syntax (avoid using reserved words or keywords in the variable or attribute);

In order to ensure that it can take an object in the properties, more practical method in square brackets.

6.ES6 new features: set data structure

Basic usage: ES6 provides a new data structure Set. It is similar to an array, but the value of the member is unique, no duplicate values.

Set itself is a constructor to generate Set data structure.

transfer:

new new the Set the SET = const ([1, 2, 3, 4, 4]);
console.log (the SET) // [1,2,3,4]
is not particularly convenient, so to say, as soon as it is to learn ES6 ~
---------------------

Guess you like

Origin www.cnblogs.com/liangyh55/p/11334645.html