1, achieve the goal: array to weight
2, realize the idea:
(1) Create a new array.
(2) Iterate over the original array, determine whether the currently traversed element exists in the new array, if present in the new array, the current traversed element is judged to be repeated, if it does not exist in the new array, it is determined that the currently traversed element is not duplicated, it is pressed into the new array.
(3) After traversing the original array, the new array is returned.
3, the concrete realization: according to the current element whether the repetition judgment method is different, has four different kinds of concrete realization.
(1) Use the hash table to save the iterated state of the traversed element.
1 functionUnique (arr) {2 varn = {},//hash table for determining whether an element has been pressed into a new array3r = [];4 for(vari = 0, length = arr.length; i < length; i++) {5 //if the current element is already pressed into the new array, the value in the hash table is true, which determines whether it repeats6 if( !n[Arr[i]]) {7n[Arr[i]] =true;8 R.push (Arr[i]);9 };Ten }; One returnR; A};
(2) Use the IndexOf method to determine whether the currently traversed element is in a new array.
1 functionUnique (arr) {2 varn = [];3 for(vari = 0, length = arr.length; i < length; i++) {4 if(N.indexof (arr[i]) = = = 1 ) {5 N.push (Arr[i]);6 };7 }8 returnN;9};
(3) Use the current traversed element to determine if its index is equal to the first occurrence of the original array.
1 functionUnique (arr) {2 varn = [];3 for(vari = 0, length = arr.length; i < length; i++) {4 //if the item I of the current array is the first occurrence in the current array that is not I, then the term I is duplicated and ignored. Otherwise, the new array is stored5 if(Arr.indexof (arr[i]) = = =i) {6 N.push (Arr[i]);7 };8 };9 returnN;Ten};
(4) Whether the position of the first occurrence of the current traversed element in the original array and the position of the last occurrence are duplicated.
1 functionUnique (arr) {2 varn = [];3 for(vari = 0, length = arr.length; i < length; i++) {4 //if the item I of the current array is the first occurrence of the position in the current array and the last occurrence is equal5 if(Arr.indexof (arr[i]) = = =Arr.lastindexof (Arr[i])) {6 N.push (Arr[i]);7 };8 };9 returnN;Ten}
(5) Using the sorting method of the array, first sorting, using the sorted array characteristics, that is, the same value of the adjacent elements, to determine whether to repeat.
1 functionUnique (arr) {2 Arr.sort ();3 varR = [];4 for(vari = 1, length = arr.length; i < length; i++) {5 if(Arr[i]!== arr[i-1] ) {6 R.push (Arr[i]);7 };8 };9 returnR;Ten};
4, Summary:
(1) Based on the array location:
Second, whether the current element appears in the new array to determine if the repetition;
Third, according to the position of the first occurrence of the current element and whether the index of the currently traversed element is equal to determine whether the repetition;
The fourth type, based on the position of the first occurrence of the current element and the last occurrence of the position to determine whether or not repeat;
(2) based on array distribution:
The first is to build a hash table, save the distribution state of the array elements, iterate over the array, and judge whether or not to repeat according to the distribution state;
(3) array-based sorting:
The fifth one is to sort the arrays, and to determine whether the elements are duplicated by using the attributes adjacent to the array elements of the same value after sorting.
JavaScript tips: Array de-weight