6 examples of 'check duplicate values in array javascript' in JavaScript

Every line of 'check duplicate values in array javascript' 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
53function testForDuplicates(obj, key, dups) {
54 test(wrap(key) + ' has no duplicate', function() {
55 var id = key.toUpperCase(),
56 exists = id in dups;
57
58 if (exists) {
59 dups[id].push(key);
60 } else {
61 dups[id] = [ key ];
62 }
63
64 assert.ok(!exists, dups[id].join(', '));
65 });
66}
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

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}
270export function getDuplicateNames(indexableArray: { name: string }[]): string[] {
271 const seen: { [name: string]: number } = {};
272 const duplicates: string[] = [];
273
274 indexableArray.forEach((element: { name: string }) => {
275 const { name } = element;
276 const count = seen[name] || 0;
277
278 if (count === 1) {
279 duplicates.push(name);
280 }
281 seen[name] = count + 1;
282 });
283 return duplicates;
284}
64function duplicateArray(array) {
65 return array.clone ? array.clone() : new Array(array.length);
66}
30function duplicateArray(arr) {
31 var newArr = [];
32 for(var i=0, arrSize=arr.length; i<arrSize; i++) {
33 newArr.push(arr[i]);
34 }
35 return newArr;
36}
108function verify_distinct(values) {
109 var arr, i, j, k, N;
110
111 if (values instanceof Array) {
112 arr = values;
113 } else if (values instanceof Object) {
114 arr = [];
115 for (k in values) {
116 arr.push(values[k]);
117 }
118 } else {
119 throw "verify_distinct: Invalid argument.";
120 }
121
122 for (i = 0, N = arr.length; i < N; ++i) {
123 for (j = 0; j < i; ++j) {
124 if (arr[i] === arr[j]) {
125 return false;
126 }
127 }
128 }
129 return true;
130}

Related snippets