10 examples of 'typescript remove duplicates from array' in JavaScript

Every line of 'typescript remove duplicates from array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
10function removeDuplicate(arr) {
11 return [...(new Set(arr.map(n => JSON.stringify(n))))].map(n => JSON.parse(n))
12}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

9public static removeDuplicates(array: any[]) {
10 if (!array) {
11 return [];
12 }
13 return array.filter((current, index) => {
14 return array.indexOf(current) === index;
15 });
16}
25function removeDuplicates_for_loop(arr) {
26 let res = [];
27 const len = arr.length;
28 for (let i = 0; i < len; i++) {
29 if (res.indexOf(arr[i]) === -1) {
30 res.push(arr[i]);
31 }
32 }
33 return res;
34}
48function removeDuplicates(_array) {
49 var _uniques = [], _dupl = [], i = 0;
50 for (i; i <= _array.length; i++) {
51 var v = _array[i];
52 console.log(i + '-' + v);
53 if (_array.lastIndexOf(v) > i || _dupl.indexOf(v) > -1) {
54 _dupl.push(v);
55 } else {
56 _uniques[_uniques.length] = v;
57 }
58 }
59
60 return $.grep(_uniques, function(n, i) {
61 return (n);
62 });
63}
21export function remove<T>(array: T[], targetIndex: number) {
22 return array.filter((_, index) => index !== targetIndex);
23}
48export function remove(array, index) {
49 var len = array.length - 1;
50
51 var out = new Array(len);
52
53 var i = 0;
54 while (i < index) {
55 out[i] = array[i];
56 ++i;
57 }
58
59 while (i < len) {
60 out[i] = array[i + 1];
61 ++i;
62 }
63
64 return out;
65}
52function horribleDuplicates (array) {
53 for (var i = 0; i < array.length; i++) {
54 for (var j = 0; j < array.length; j++) {
55 if (array[i] == array[j]) {
56 return true
57 }
58 }
59 }
60 return false
61}
483function remove(array, item) {
484 array.splice(array.indexOf(item), 1);
485
486 return array;
487}
7function dedupe (array) {
8 return Array.from(new Set(array));
9 }
23function removeDuplicates(myArr, prop) {
24 return myArr.filter((obj, pos, arr) => {
25 return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
26 });
27}

Related snippets