The method of a filter () + indexOf ()
@ Ideas: the use of the main filter () method to filter out duplicate elements function ArrayToHeavy (ARR) { // filter out duplicate numbers in the original array, a new array returned return arr.filter ((Item, index) => { // iterate the array number first appears index, compared to the array index where the numbers, // is true that the first occurrence of return arr.indexOf (Item) === index }) } the let ARR = [1,21,2,24,3,3,7,4,4,5,5 ] the console.log (ArrayToHeavy (ARR)) // printing is 1, 21, 2, 24, 3, 7, 4, 5
Method two push () + indexOf ()
@ Idea: mainly using the indexOf () method of determining whether the passed array value is present in the new array, the absence of traditional values put into the new array push function ArrayToHeavy (ARR) { // Create an empty array the let newArr = [ ]; for ( var I = 0; I <arr.length; I ++ ) { // iterate array passed, to find whether there is the incoming value of the new array array iF (newArr.indexOf (ARR [I]) == = -1 ) { // do not exist to push the new values put array newArr.push (ARR [I]); } } // returns a new array return newArr; } the let a = [1,1,2,3, 4,5,6,4,6,8,65,77 ]; the console.log (ArrayToHeavy (A)); //Printing is [1, 2, 3, 4, 5, 6, 8, 65, 77] // ******************************************************** ************************************************** ** // // idea: mainly the use of indexOf () method to find the value of the array passed in the first occurrence of the subscript, subscript use to find out the numbers to find the first occurrence and push it to the new array function ArrayToHeavy (ARR) { // Create an empty array the let newArr = []; for ( var I = 0; I <arr.length; I ++ ) { // iterate array passed, the value of the incoming lookup array one occurrence of the subscript IF (arr.indexOf (ARR [I]) === I) { // Push occurrence incoming digital array newArr.push (ARR [I]); } } // returns a new array return newArr; } the let a= [1,1,2,3,4,5,6,4,6,8,65,77]; console.log(ArrayToHeavy(a));
A method for three double loop + splice () or double for loop + Push ()
@ Ideas: for using a double loop is repeated to find an element, then use splice () method to delete a duplicate function ArrayToHeavy (ARR) { // iterate all the elements for ( var I = 0, len = ARR. length; I <len; I ++ ) { for ( var V = I +. 1; V <len; V ++ ) { // check for duplicate elements iF (ARR [I] === ARR [V]) { // there, it is removed from the array arr.splice (v,. 1 ); // splice method changes the length of the array, and so to the array length len minus one subscript v len-- ; v - ; } } } return ARR } the let A = [2,4,5,7,4,8,0,4,5,7,9,4,5,21 ]; the console.log (ArrayToHeavy (A)); // Print [ 2, 4, 5, 7, 8, 0, 9, 21] // ******************************** ************************************************** ********** @ ideas: for using a double loop, the outer loop does not acquire new values into an array of repeating the loop is terminated at the same time into the next cycle when the top inner loop detects a repetitive elements Analyzing round function ArrayToHeavy (ARR) { the let newArray = []; // iterate all the elements for ( var I = 0, len = arr.length; I <len; I ++ ) { for ( var V = I +. 1 ; V <len; V ++ ) { // detects the termination of the current cycle is repeated when the same time into the top loop element at a determined IF (ARR [I] === ARR [V]) { I ++ ; V = I; } } // Get not repeated values into the new array newArray.push (ARR [I]) } return newArray } the let a = [2,4,5,7,4,8,0,4,5,7,9,4,5, 21 ]; the console.log (ArrayToHeavy (A)); // print [2, 8, 0, 7, 9, 4, 5, 21]