I have a question. How do you retrieve elements that do not have a double value in an array? For example: [1,1,2,2,3,4,4,5], then you extract only [3,5]. thanks in advance
for (var j = 0; j < newArr.length; j++) { if ((arr1.indexOf(newArr[j]) === 0) && (arr2.indexOf(newArr[j]) === 0)) { index = newArr.indexOf(j); newArr.splice(index, 1); } }
If the element in the array is unique, then the index found from the very beginning should equal the index found from the end, in other words:
var xs = [1, 1, 2, 2, 3, 4, 4, 5]; var result = xs.filter(function(x) { return xs.indexOf(x) === xs.lastIndexOf(x); }); console.log(result); //=> [3, 5]
create a new array tmpand check the existing value indexOf. If there was a deletion using the function splice.
tmp
indexOf
splice
var arr = [1,1,2,2,3,4,4,5]; var tmp = []; var dup = []; for(var i = 0; i < arr.length; i++){ var ind = tmp.indexOf(arr[i]); if(ind == -1){ if(dup.indexOf(arr[i]) == -1){ tmp.push(arr[i]); } } else{ tmp.splice(ind,1); dup.push(arr[i]); } } console.log(tmp);
- . O (2n)
var array = [1, 1, 2, 2, 3, 4, 4, 5, 1], hash = Object.create(null), single; array.forEach(function (a, i) { hash[a] = a in hash ? -1 : i; }); single = array.filter(function (a, i) { return hash[a] === i; }); console.log(single);
! ,
var tab = [1,1,2,2,3,4,4,5] //The array to analyze tab = tab.sort(); // we sort the array show(tab); // we display the array to the console (F12 to open it) var uniqueElementTab = []; // this array will contain all single occurence var sameElementCounter = 0; for(x=0;x<tab.length;x++){ // for all element in the array sameElementCounter = 0; for(y=0;y<tab.length;y++){ // we compare it to the others if((tab[x]==tab[y])){ sameElementCounter+=1; // +1 each time we meet the element elsewhere } } if(sameElementCounter<=1){ uniqueElementTab.push(tab[x]); //if the element is unique we add it to a new array } } show(uniqueElementTab); // display result function show(tab) { // Simple function to display the content of an array var st=""; for(i=0;i<tab.length;i++){ st += tab[i]+" "; } console.log(st+"\n"); }
, .
"" Array.sort, Array.join, Array.map, String.replace String.split:
Array.sort
Array.join
Array.map
String.replace
String.split
var arr = [1, 1, 2, 2, 3, 4, 4, 5]; arr.sort(); var unique = arr.join("").replace(/(\d)\1+/g, "").split("").map(Number); console.log(unique); // [3, 5]
.
var arr = [1,1,2,2,3,4,4,5], uniques = Object.keys(arr.reduce((p,c) => (c in p ? Object.defineProperty(p, c, {enumerable : false, writable : true, configurable : true}) : p[c] = c, p), {})); console.log(uniques);
, O (n) (. "pushUniqueSinglePass" ):
function pushUniqueSinglePass(array, unique) { var prev; // last element seen var run = 0; // number of times it has been seen for (var i = 0; i < array.length; i++) { if (array[i] != prev) { if (run == 1) { unique.push(prev); // "prev" appears only once } prev = array[i]; run = 1; } else { run++; } } } function pushUniqueWithSet(array, unique) { var set = new Set(); for (var i = 0; i < array.length; i++) { set.add(array[i]); } for (let e of set) { unique.push(set); } } // Utility and test functions function randomSortedArray(n, max) { var array = []; for (var i = 0; i < n; i++) { array.push(Math.floor(max * Math.random())); } return array.sort(); } function runtest(i) { var array = randomSortedArray(i, i / 2); var r1 = [], r2 = []; console.log("Size: " + i); console.log("Single-pass: " + time( pushUniqueSinglePass, array, r1)); console.log("With set: " + time( pushUniqueWithSet, array, r2)); // missing - assert r1 == r2 } [10, 100, 1000, 10000, 100000, 1000000 ].forEach(runtest); function time(fun, array, unique) { var start = new Date().getTime(); fun(array, unique); return new Date().getTime() - start; }
, (!). 1 , 18 ; , , 10 .