Javascript Array myUniq()
method
var arr = [5,4,3,2,1,2,3,4,5]; Array.prototype.myUniq = function () { let uniq = [];//from w w w . j av a2s.co m for(let i = 0; i < this.length; i+=1) { if (uniq.includes(this[i])) { continue; } else { uniq.push(this[i]); } } return uniq; } console.log(arr.my_uniq());
var arr = ['string', 'water',1,2,'string',2]; Array.prototype.myUniq = function () { let uniq = [];//from w w w. j a v a 2 s .c o m for(let i = 0; i < this.length; i+=1) { if (uniq.includes(this[i]) === false) { uniq.push(this[i]); } } return uniq; } console.log(arr.myUniq());
'use strict';//from w w w.j a v a2 s.c o m Array.prototype.myUniq = function () { let results = []; for (let i = 0; i < this.length; i+=1){ if (results.includes(this[i])){ continue; } else { results.push(this[i]); } } return results; }; console.log([1, 2, 1, 3, 3].myUniq());
Array.prototype.myUniq = function () { var uniques = []; for (var i = 0; i < this.length; i++) { if ( uniques.indexOf(this[i]) === -1 ) { uniques.push(this[i]);/*from w ww .j a va 2 s . com*/ } } return uniques; }; function twoSum(arr) { var sums = []; for (var i = 0; i < arr.length; i++) { for (var j = (i + 1); j < arr.length; j++) { if ( arr[i] + arr[j] === 0 ) { sums.push([i, j]); } } } return sums; } function myTranspose(arr) { var transposed = []; for (var row = 0; row < arr.length; row++) { transposed.push([]); // debugger for (var i = 0; i < arr[row].length; i++) { transposed[row][i] = arr[i][row]; } } return transposed; } var rows = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ]; // console.log( myTranspose(rows) ); module.exports = myUniq;
Array.prototype.myUniq = function () { const result = [];/*from ww w . j a va2 s. c o m*/ for (let i = 0; i < this.length; i++) { if (!result.includes(this[i])) { result.push(this[i]); } } return result; };