var arr = [1,1,2,1,3,4,5]; var set = new Set(arr); console.log(Array.from(set))
function norepeat(arr){ var newarr = []; for(var i in arr){ if(newarr.indexOf(arr[i]) == -1){ newarr.push(arr[i]); } } return newarr; }
function norepeat(arr){ var newarr = [arr[0]]; //默認加入第一個數 由於第一個數沒比較的必要 for(var i =1;i<arr.length;i++){ if(arr.indexOf(arr[i])==i){ newarr.push(arr[i]) } } return newarr; }
var res = []; var obj = {}; for(var i=0; i<arr.length; i++){ if( !obj[arr[i]] ){ obj[arr[i]] = 1; res.push(arr[i]); }}
function norepeat(arr) { arr.sort(function (a, b) { return a - b; }); for (var i = 0; i < arr.length; i++) { if (arr[i] == arr[i + 1]) { arr.splice(i, 1); i--; } } return arr; }
function norepeat(arr){ for(var i = 0;i < arr.length-1;i++){ for(var j = i+1;j<arr.length;j++){ if(arr[i] == arr[j]){ arr.splice(j,1); j--; } } } return arr; }
function norepeat(arr){ var newArr = arr; for(var i = newArr.length;i > 0 ; i--){ for(var j = 0; j<i ; j++){ if(newArr[i] == newArr[j]){ newArr.splice(i,1); } } } return arr; }
function norepeat(arr){ var newArr = []; var arrs = []; for(var i=0;i<arr.length;i++){ var a = arr[i]; newArr[a] = 1; } for(var i in newArr){ arrs[arrs.length] = i; console.log(i); } }
function Digui(arr) { // 獲取長度 var len = arr.length; //對數組進行排序才能方便比較 arr.sort(function (a, b) { return a - b; }) // 用遞歸的方法進行去重 function loop(index) { if (index >= 1) { if (arr[index] === arr[index - 1]) { arr.splice(index, 1); } loop(index - 1); //遞歸loop函數進行去重 } } loop(len - 1); return arr; };
var arr = [6, 1, 1, 2, 1, 2, 1, 1, 3, 1, 1, 1, 4, 15]; function norepeat(arr) { var arrcopy = []; for (var i = 0; i < arr.length; i++) { var count = 0; for (var j in arrcopy) { if (arrcopy[j] != arr[i]) { count++; } } console.log(arrcopy); if (count == arrcopy.length) { arrcopy[arrcopy.length] = arr[i]; } } return arrcopy; }