1npm i find-array-duplicates
2
3import duplicates from 'find-array-duplicates'
4
5const names = [
6 { 'age': 36, 'name': 'Bob' },
7 { 'age': 40, 'name': 'Harry' },
8 { 'age': 1, 'name': 'Bob' }
9]
10
11duplicates(names, 'name').single()
12// => { 'age': 36, 'name': 'Bob' }
1// 287. Find the Duplicate Number
2// Medium
3
4// Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
5
6// Example 1:
7
8// Input: [1,3,4,2,2]
9// Output: 2
10// Example 2:
11
12// Input: [3,1,3,4,2]
13// Output: 3
14// Note:
15
16// You must not modify the array (assume the array is read only).
17// You must use only constant, O(1) extra space.
18// Your runtime complexity should be less than O(n2).
19// There is only one duplicate number in the array, but it could be repeated more than once.
20
21class Solution {
22public:
23 int findDuplicate(vector<int>& nums) {
24 int n=nums.size();
25 int s=nums[0];
26 int f=nums[nums[0]];
27 while(s!=f) {
28 s=nums[s];
29 f=nums[nums[f]];
30 }
31 f=0;
32 while(s!=f) {
33 s=nums[s];
34 f=nums[f];
35 }
36 return s;
37
38 }
39};
1/*This method is all in one
2*you can find following things:
3*finding Duplicate elements in array
4*array without duplicate elements
5*number of duplicate elements
6*numbers of pair of dulicate with repeatation
7*/
8//let given array = [2,3,2,5,3]
9 public static void findDuplicateArray(int [] array)
10 {
11 int size = array.length;
12 //creating array to hold count frequency of array elements
13 int [] countFrequency = new int[size];
14 // filling countFrequency with -1 value on every index
15 for(int i = 0; i < size; i++)
16 {
17 countFrequency[i] = -1;//[-1,-1,-1,-1,-1...]
18 }
19
20 int count = 1;
21 for(int i = 0; i < size; i++)
22 {
23 //check countFrequency[i] != 0 because 0 means it already counted
24 if(countFrequency[i] != 0)
25 {
26 for(int j = i+1; j < size; j++)
27 {
28 //if array[i] == array[j] then increase count value
29 if(array[i] == array[j])
30 {
31 count++;
32 /*only at first occurence of an element count value
33 *will be increased else everywhere it will be 0
34 */
35 countFrequency[j]= 0;
36 }
37 }
38 countFrequency[i] = count;
39 }
40 count = 1;
41 }
42 // array = [2,3,2,5,3]
43 //countFrequency = [2,2,0,1,0]
44 System.out.println("array without duplicate elements");
45 for(int i = 0; i < array.length; i++)
46 {
47 if(countFrequency[i] >= 1)
48 System.out.print(array[i] + " ");
49 }
50 System.out.println();
51
52 System.out.println("duplicate elements in array");
53 for(int i = 0; i < array.length; i++)
54 {
55 if(countFrequency[i]/2 >= 1)
56 System.out.print(array[i] + " ");
57 }
58 System.out.println();
59
60 System.out.println("number of duplicate elements");
61 count = 0;
62 for(int i = 0; i < array.length; i++)
63 {
64 if(countFrequency[i]/2 >= 1)
65 count++;
66 }
67 System.out.print(count);
68 System.out.println();
69
70 System.out.println("numbers of pair of dulicate with repeatation");
71 count = 0;
72 for(int i = 0; i < array.length; i++)
73 {
74 if(countFrequency[i] >= 2)
75 {
76 int div = countFrequency[i]/2;
77 count+=div;
78 }
79 }
80 System.out.println(count);
81
82 int [] array3 = new int [array.length];
83 for(int i = 0; i < array.length; i++)
84 {
85 for(int j = 0; j < countFrequency[i]; j++)
86 {
87 array3[i]= array[i];
88 }
89 }
90 }