0% found this document useful (0 votes)
9 views12 pages

DSA Project

Uploaded by

verdigris547
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

DSA Project

Uploaded by

verdigris547
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

To solve this project, we need to implement **three different methods** to find

the **most frequent duplicate element** in a list of integers. Additionally, we


need to provide a comparison of their performance, including a time complexity
analysis.

### Steps to solve the problem:

1. **Understand the problem**:


- We are given a list of integers, and we need to find the most frequent
duplicate element.
- If there are multiple elements with the same frequency, return the one that
appears first.
- The number of integers in the list is less than \(10^5\), and each integer is
within the range \((-10^5, 10^5)\).

2. **Clarify the requirements**:


- Implement at least three different methods.
- Analyze the time complexity for each method.
- Test your solutions on different input data and compare their performance.

### Plan:

We will implement three different methods and compare their time complexities:

1. **Method 1: Brute Force Approach (O(n²))**


- Use a nested loop to count the frequency of each element and then find the
most frequent duplicate.

2. **Method 2: Hash Map Approach (O(n))**


- Use a hash map (or dictionary) to store the frequency of each element. Then,
iterate over the hash map to find the most frequent duplicate.

3. **Method 3: Sorting-Based Approach (O(n log n))**


- Sort the list and count the frequency of each element while traversing the
sorted list.
---

### Method 1: Brute Force Approach

Let's start with the brute force approach. This method uses a nested loop to
count the frequency of each element.

#### Algorithm:
1. For each element in the list, count how many times it appears in the list.
2. Track the element with the highest frequency.
3. Return the most frequent duplicate element.

#### Code (in C++):

```cpp
#include <iostream>
#include <vector>
using namespace std;

// Function to find the most frequent duplicate using brute force


int bruteForceMostFrequentDuplicate(const vector<int>& nums) {
int mostFrequent = -1;
int maxCount = 0;

for (int i = 0; i < nums.size(); i++) {


int count = 0;
for (int j = 0; j < nums.size(); j++) {
if (nums[i] == nums[j]) {
count++;
}
}
if (count > maxCount && count > 1) {
maxCount = count;
mostFrequent = nums[i];
}
}

return mostFrequent;
}

int main() {
vector<int> nums = {7, 4, 5, 9, 5, 8, 3, 3, 5};
cout << "Brute Force Result: " << bruteForceMostFrequentDuplicate(nums)
<< endl;
return 0;
}
```

#### Time Complexity:


- **Worst-case time complexity**: \(O(n^2)\), where \(n\) is the number of
elements in the list.
- The nested loop makes this approach inefficient for large lists.

---

### Method 2: Hash Map Approach

This approach uses a hash map (or dictionary) to store the frequency of each
element. It is more efficient than the brute force approach.

#### Algorithm:
1. Use a hash map to store the frequencies of each element in the list.
2. Iterate through the hash map to find the element with the highest frequency.
3. Return the most frequent duplicate element.
#### Code (in C++):

```cpp
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

// Function to find the most frequent duplicate using a hash map


int hashMapMostFrequentDuplicate(const vector<int>& nums) {
unordered_map<int, int> frequency;
int mostFrequent = -1;
int maxCount = 0;

// Count the frequency of each element


for (int num : nums) {
frequency[num]++;
}

// Find the most frequent duplicate


for (int num : nums) {
if (frequency[num] > maxCount && frequency[num] > 1) {
maxCount = frequency[num];
mostFrequent = num;
}
}

return mostFrequent;
}

int main() {
vector<int> nums = {7, 4, 5, 9, 5, 8, 3, 3, 5};
cout << "Hash Map Result: " << hashMapMostFrequentDuplicate(nums) <<
endl;
return 0;
}
```

#### Time Complexity:


- **Worst-case time complexity**: \(O(n)\), where \(n\) is the number of elements
in the list.
- Each element is processed once to build the frequency map and once to find
the most frequent duplicate.

---

### Method 3: Sorting-Based Approach

This method sorts the list first and then counts the frequency of each element by
traversing the sorted list.

#### Algorithm:
1. Sort the list.
2. Traverse the sorted list and count the frequency of each element.
3. Keep track of the element with the highest frequency and return it.

#### Code (in C++):

```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to find the most frequent duplicate using sorting
int sortingMostFrequentDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());

int mostFrequent = -1;


int maxCount = 0;
int currentCount = 1;

for (int i = 1; i < nums.size(); i++) {


if (nums[i] == nums[i - 1]) {
currentCount++;
} else {
if (currentCount > maxCount && currentCount > 1) {
maxCount = currentCount;
mostFrequent = nums[i - 1];
}
currentCount = 1;
}
}

// Check the last group


if (currentCount > maxCount && currentCount > 1) {
mostFrequent = nums[nums.size() - 1];
}

return mostFrequent;
}

int main() {
vector<int> nums = {7, 4, 5, 9, 5, 8, 3, 3, 5};
cout << "Sorting Result: " << sortingMostFrequentDuplicate(nums) << endl;
return 0;
}
```

#### Time Complexity:


- **Worst-case time complexity**: \(O(n \log n)\), where \(n\) is the number of
elements in the list.
- Sorting the list takes \(O(n \log n)\), and traversing the sorted list takes \
(O(n)\).

---

### Conclusion and Performance Comparison

1. **Brute Force Approach**:


- Time Complexity: \(O(n^2)\)
- Simple to implement, but inefficient for large lists.

2. **Hash Map Approach**:


- Time Complexity: \(O(n)\)
- Efficient and straightforward using hash maps.

3. **Sorting-Based Approach**:
- Time Complexity: \(O(n \log n)\)
- Slightly less efficient than the hash map approach, but still better than brute
force.

#### Which method should you choose?


- For large input sizes, the **hash map approach** is the most efficient, as it
operates in linear time.
- The **sorting-based approach** is also reasonably efficient, but it is slightly
slower due to the sorting step.
- The **brute force approach** should generally be avoided for large inputs as it
has quadratic time complexity.
#### Testing:

You can design test cases with different input sizes and distributions of duplicates
to evaluate the performance of each method. For example, you might want to
test:
- Lists with no duplicates.
- Lists with many duplicates.
- Lists with varying sizes (e.g., 10 elements, 100 elements, 100,000 elements).

Don't forget to submit the required materials (codes, screenshots, and final
report) according to the project instructions.
/*****************************************
* (This comment block is added by the Judge System)
* Submission ID: 301768
* Submitted at: 2024-11-13 23:08:29
*
* User ID: 2556
* Username: 58499294
* Problem ID: 897
* Problem Name: Find The Most Frequent Duplicate
*/

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main() {

string line;

while (getline(cin, line)) {


stringstream ss(line);
vector<int> arr;
int n;

while (ss >> n) {


arr.push_back(n);
}

int mostfreq = -1;


int max_frequency = 0;

for (int i = 0; i < arr.size(); i++) {


int c = 0;
for (int j = 0; j < arr.size(); j++) {
if (arr[i] == arr[j]) {
c++;
}
}
if (c > max_frequency) {
max_frequency = c;
mostfreq = arr[i];
}
}
cout << mostfreq << endl;}

return 0;
}

/*****************************************
* (This comment block is added by the Judge System)
* Submission ID: 301771
* Submitted at: 2024-11-13 23:18:50
*
* User ID: 2556
* Username: 58499294
* Problem ID: 897
* Problem Name: Find The Most Frequent Duplicate
*/

#include <iostream>
#include <vector>
#include <sstream>
#include <unordered_map>
using namespace std;

int main() {

string line;

while (getline(cin, line)) {


stringstream ss(line);
vector<int> arr;
int n;

while (ss >> n) {


arr.push_back(n);
}

unordered_map<int,int>num;
int mostfreq = -1;
int max_frequency = 0;
for(int i=0 ; i<arr.size(); i++){
num[arr[i]]++;
}

for(int i=0; i< arr.size(); i++){


if(num[arr[i]] > max_frequency){
max_frequency = num[arr[i]];
mostfreq = arr[i];
}
}
cout << mostfreq << endl;}

return 0;
}

You might also like