• API
• FAQ
• Tools
• Archive
daily pastebin goal
37%
SHARE
TWEET

# Untitled

a guest Nov 20th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function eliminateDuplicates(arr) {
2.   var i,
3.       len=arr.length,
4.       out=[],
5.       obj={};
6.
7.   for (i=0;i<len;i++) {
8.     obj[arr[i]]=0;
9.   }
10.   for (i in obj) {
11.     out.push(i);
12.   }
13.   return out;
14. }
15.
16. var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
17.
18. var uniq = names
19. .map((name) => {
20.   return {count: 1, name: name}
21. })
22. .reduce((a, b) => {
23.   a[b.name] = (a[b.name] || 0) + b.count
24.   return a
25. }, {})
26.
27. var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
28.
29. console.log(duplicates) // [ 'Nancy' ]
30.
31. const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
32.
33. const count = names =>
34.   names.reduce((a, b) =>
35.     Object.assign(a, {[b]: (a[b] || 0) + 1}), {})
36.
37. const duplicates = dict =>
38.   Object.keys(dict).filter((a) => dict[a] > 1)
39.
40. console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
41. console.log(duplicates(count(names))) // [ 'Nancy' ]
42.
43. // prep
45.   const counter = (prev, next) => Object.assign(prev, { [next] : (prev[next] || 0) + 1 });
46.   const singles = function(key){ return this[key] === 1 };
47.   const multiples = function(key){ return this[key] > 1 };
48.
49.   // work
50.   const counted = arr.reduce(counter, {});
51.   const filtered = Object.keys(counted).filter(multiples.bind(counted));
52.
53.   //[ "e", "a", "r", " ", "d" ]
54.   console.log(filtered);
55.
56. Array.prototype.unique = function () {
57.     var r = new Array();
58.     o:for(var i = 0, n = this.length; i < n; i++)
59.     {
60.         for(var x = 0, y = r.length; x < y; x++)
61.         {
62.             if(r[x]==this[i])
63.             {
65.                 continue o;
66.             }
67.         }
68.         r[r.length] = this[i];
69.     }
70.     return r;
71. }
72.
73. var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
74. var unique = arr.unique();
76.
77. var unique = function(){
78.   var hasOwn = {}.hasOwnProperty,
79.       toString = {}.toString,
80.       uids = {};
81.
82.   function uid(){
83.     var key = Math.random().toString(36).slice(2);
84.     return key in uids ? uid() : uids[key] = key;
85.   }
86.
87.   function unique(array){
88.     var strings = {}, numbers = {}, others = {},
89.         tagged = [], failed = [],
90.         count = 0, i = array.length,
91.         item, type;
92.
93.     var id = uid();
94.
95.     while (i--) {
96.       item = array[i];
97.       type = typeof item;
98.       if (item == null || type !== 'object' && type !== 'function') {
99.         // primitive
100.         switch (type) {
101.           case 'string': strings[item] = true; break;
102.           case 'number': numbers[item] = true; break;
103.           default: others[item] = item; break;
104.         }
105.       } else {
106.         // object
107.         if (!hasOwn.call(item, id)) {
108.           try {
109.             item[id] = true;
110.             tagged[count++] = item;
111.           } catch (e){
112.             if (failed.indexOf(item) === -1)
113.               failed[failed.length] = item;
114.           }
115.         }
116.       }
117.     }
118.
119.     // remove the tags
120.     while (count--)
121.       delete tagged[count][id];
122.
123.     tagged = tagged.concat(failed);
124.     count = tagged.length;
125.
126.     // append primitives to results
127.     for (i in strings)
128.       if (hasOwn.call(strings, i))
129.         tagged[count++] = i;
130.
131.     for (i in numbers)
132.       if (hasOwn.call(numbers, i))
133.         tagged[count++] = +i;
134.
135.     for (i in others)
136.       if (hasOwn.call(others, i))
137.         tagged[count++] = others[i];
138.
139.     return tagged;
140.   }
141.
142.   return unique;
143. }();
144.
145. function unique(array){
146.   var seen = new Set;
147.   return array.filter(function(item){
148.     if (!seen.has(item)) {
150.       return true;
151.     }
152.   });
153. }
154.
155. function find_duplicates(arr) {
156.   var len=arr.length,
157.       out=[],
158.       counts={};
159.
160.   for (var i=0;i<len;i++) {
161.     var item = arr[i];
162.     counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
163.     if (counts[item] === 2) {
164.       out.push(item);
165.     }
166.   }
167.
168.   return out;
169. }
170.
171. find_duplicates(['one',2,3,4,4,4,5,6,7,7,7,'pig','one']); // -> ['one',4,7] in no particular order.
172.
173. function hasDuplicate(arr){
174.     return (arr.length != _.uniq(arr).length);
175. }
176.
177. var a = ["a","a","b","c","c"];
178.
179. a.filter(function(value,index,self){ return (self.indexOf(value) !== index )})
180.
181. var a = [324,3,32,5,52,2100,1,20,2,3,3,2,2,2,1,1,1].sort();
182. a.filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});
183.
184. //copy and paste: without error handling
185. Array.prototype.unique =
186.    function(){return this.sort().filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});}
187.
188. [1, 2, 3].every(function(elem, i, array){return array.lastIndexOf(elem) === i}) // true
189.
190. [1, 2, 1].every(function(elem, i, array){return array.lastIndexOf(elem) === i}) // false
191.
192. Array::duplicates = -> not @every((elem, i, array) -> array.lastIndexOf(elem) is i)
193.
194. [1, 2, 3].duplicates() // false
195. [1, 2, 1].duplicates() // true
196.
197. Array.prototype.unique = function () {
198.     var arr = this.sort(), i; // input must be sorted for this to work
199.     for( i=arr.length; i--; )
200.       arr[i] === arr[i-1] && arr.splice(i,1); // remove duplicate item
201.
202.     return arr;
203. }
204.
205. var arr =  [1,2,2,3,3,4,5,6,2,3,7,8,5,9],
206.     arr2 = [1,2,511,12,50],
207.     arr3 = [22],
208.     unique = arr.concat(arr2, arr3).unique();
209.
210. console.log(unique);  // [22, 50, 12, 511, 2, 1, 9, 5, 8, 7, 3, 6, 4]
211.
212. if (!Array.prototype.indexOf){
213.    Array.prototype.indexOf = function(elt /*, from*/){
214.      var len = this.length >>> 0;
215.
216.      var from = Number(arguments[1]) || 0;
217.      from = (from < 0) ? Math.ceil(from) : Math.floor(from);
218.      if (from < 0)
219.         from += len;
220.
221.      for (; from < len; from++){
222.         if (from in this && this[from] === elt)
223.            return from;
224.      }
225.      return -1;
226.   };
227. }
228.
229. if( \$.inArray(this[i], arr) == -1 )
230.
231. function findDuplicates(arr) {
232.     var i,
233.         len=arr.length,
234.         out=[],
235.         obj={};
236.
237.     for (i=0;i<len;i++) {
238.         if (obj[arr[i]] != null) {
239.             if (!obj[arr[i]]) {
240.                 out.push(arr[i]);
241.                 obj[arr[i]] = 1;
242.             }
243.         } else {
244.             obj[arr[i]] = 0;
245.         }
246.     }
247.     return out;
248. }
249.
250. var codes = dc_1.split(',');
251. var i = codes.length;
252. while (i--) {
253.   if (codes.indexOf(codes[i]) != i) {
254.     codes.splice(i,1);
255.   }
256. }
257.
258. var arrayToFilter = [ 4, 5, 5, 5, 2, 1, 3, 1, 1, 2, 1, 3 ];
259.
260. arrayToFilter.
261.     sort().
262.     filter( function(me,i,arr){
263.        return (i===0) || ( me !== arr[i-1] );
264.     });
265.
266. var input = ['a', 'b', 'a', 'c', 'c'],
267.     duplicates = [],
268.     i, j;
269. for (i = 0, j = input.length; i < j; i++) {
270.   if (duplicates.indexOf(input[i]) === -1 && input.indexOf(input[i], i+1) !== -1) {
271.     duplicates.push(input[i]);
272.   }
273. }
274.
275. console.log(duplicates);
276.
277. function identifyDuplicatesFromArray(arr) {
278.         var i;
279.         var len = arr.length;
280.         var obj = {};
281.         var duplicates = [];
282.
283.         for (i = 0; i < len; i++) {
284.
285.             if (!obj[arr[i]]) {
286.
287.                 obj[arr[i]] = {};
288.
289.             }
290.
291.             else
292.             {
293.                 duplicates.push(arr[i]);
294.             }
295.
296.         }
297.         return duplicates;
298.     }
299.
300. function GetDuplicates(arr) {
301.   var i, out=[], obj={};
302.   for (i=0; i < arr.length; i++)
303.     obj[arr[i]] == undefined ? obj[arr[i]] ++ : out.push(arr[i]);
304.   return out;
305. }
306.
307. function GetDuplicates(arr) {
308.   var i, out=[], obj={};
309.   for (i=0; i < arr.length; i++)
310.     obj[arr[i]] == undefined ? obj[arr[i]] ++ : out.push(arr[i]);
311.   return GetUnique(out);
312. }
313.
314. function GetUnique(arr) {
315.   return \$.grep(arr, function(elem, index) {
316.     return index == \$.inArray(elem, arr);
317.   });
318. }
319.
320. var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
321.
322. var obj={};
323.
324. for(var i=0;i<arr.length;i++){
325.     if(!obj[arr[i]]){
326.         obj[arr[i]]=1;
327.     } else {
328.         obj[arr[i]]=obj[arr[i]]+1;
329.     }
330. }
331. var result=[]
332. for(var key in obj){
333.     if(obj[key]>1){
334.         result.push(Number(key)) // change this to result.push(key) to find duplicate strings in an array
335.     }
336. }
337.
338. console.log(result)
339.
340. function toUnique(a,b,c){//array,placeholder,placeholder
341.  b=a.length;
342.  while(c=--b)while(c--)a[b]!==a[c]||a.splice(c,1)
343. }
344. var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];
345. toUnique(array);
346. console.log(array);
347.
348. function theDuplicates(a,b,c,d){//array,placeholder,placeholder
349.  b=a.length,d=[];
350.  while(c=--b)while(c--)a[b]!==a[c]||d.push(a.splice(c,1))
351. }
352. var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];
353.
354. toUnique(theDuplicates(array));
355.
356. function GetDuplicates(arr) {
357.     var i = 0, m = [];
358.     return arr.filter(function (n) {
359.         return !m[n] * ~arr.indexOf(n, m[n] = ++i);
360.     });
361. }
362.
363. function findDuplicate(arr) {
364. var set = new Set();
365. var duplicates = new Set();
366.   for (let i = 0; i< arr.length; i++) {
367.      var size = set.size;
369.      if (set.size === size) {
371.      }
372.   }
373.  return duplicates;
374. }
375.
376. var arr = ['a','b','c','d','a','b','c','d'];
377. var newA = [];
378. for(var i = 0; i < arr.length; i++){
379.     if(newA.indexOf(arr[i]) === -1){
380.         newA.push(arr[i]);
381.     }
382.  }
383.
384. let duplicates = a => {d=[]; a.sort((a,b) => a-b).reduce((a,b)=>{a==b&&!d.includes(a)&&d.push(a); return b}); return d};
385.
386. duplicates([1,2,3,10,10,2,3,3,10]);
387.
388. var itemcounts = _.countBy(numbers, function (n) { return n; });
389. var dupes = _.reduce(itemcounts, function (memo, item, idx) {
390.     if (item > 1)
391.         memo.push(idx);
392.     return memo;
393. }, []);
394.
395. function getDuplicates( arr ) {
396.   var i, value;
397.   var all = {};
398.   var duplicates = [];
399.
400.   for( i=0; i<arr.length; i++ ) {
401.     value = arr[i];
402.     if( all[value] ) {
403.       duplicates.push( value );
404.       all[value] = false;
405.     } else if( typeof all[value] == "undefined" ) {
406.       all[value] = true;
407.     }
408.   }
409.
410.   return duplicates;
411. }
412.
413. function getDuplicates( arr ) {
414.   var all = {};
415.   return arr.reduce(function( duplicates, value ) {
416.     if( all[value] ) {
417.       duplicates.push(value);
418.       all[value] = false;
419.     } else if( typeof all[value] == "undefined" ) {
420.       all[value] = true;
421.     }
422.     return duplicates;
423.   }, []);
424. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top