Skip to content

Commit ca33f71

Browse files
authored
Create 25 b. Find Duplicate in unsorted array using hash table.cpp
1 parent 6370f1c commit ca33f71

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include<iostream>
2+
using namespace std;
3+
struct Array {
4+
int* A;
5+
int size;
6+
int length;
7+
};
8+
9+
10+
void Count_duplicate_unsorted_hash(struct Array* arr) {
11+
int low = Min(arr);
12+
int high = Max(arr);
13+
int* H;
14+
H = new int[high];;
15+
for (int i = 0; i < high; i++) H[i]={ 0 }; // initialise hash table with zero
16+
for (int i = 0; i < arr->length; i++)
17+
H[arr->A[i]]++; // for every element go to that particular index and make increment everytime
18+
for (int i = low; i <= high; i++) // traverse through the whole hash table and check the elements
19+
{
20+
21+
if (H[i] > 1) // if hash table elements is greater then 1 that is there is the duplicate element in the array
22+
cout << i << " is appearing for " << H[i] << " Times" << endl; // print the duplicate and also print its frequency
23+
}
24+
25+
}
26+
27+
int main() {
28+
struct Array arr;
29+
int no;
30+
cout << "Enter the size of the array " << endl;
31+
cin >> arr.size;
32+
arr.A = new int[arr.size];
33+
arr.length = 0;
34+
cout << "Enter the size of the array" << endl;
35+
cin >> no;
36+
cout << "Enter the elements of the array " << endl;
37+
for (int i = 0; i < no; i++)
38+
cin >> arr.A[i];
39+
arr.length = no;
40+
41+
Count_duplicate_unsorted_hash(&arr);
42+
43+
44+
// Display(arr);
45+
return 0;
46+
47+
}

0 commit comments

Comments
 (0)