C Examples Duplicate Number
Here the c program for find the duplicate number
Code: Find the duplicate number and delete the number in Array
- Can be used in data processing where unique values are needed.
- Useful in student marks, sensor data, inventory numbers, or IDs.
- Demonstrates array manipulation, nested loops, and in-place removal.
#include <stdio.h>
int main() {
int size, i, j, k;
printf("Enter the size of the array: ");
scanf("%d", &size);
int Arr[size];
printf("Enter array elements one by one:\n");
for(i = 0; i < size; i++)
scanf("%d", &Arr[i]);
printf("\nOriginal Array: ");
for(i = 0; i < size; i++)
printf("%d\t", Arr[i]);
// Removing duplicates
for(i = 0; i < size; i++) {
for(j = i + 1; j < size; j++) {
if(Arr[i] == Arr[j]) {
// Shift elements left
for(k = j; k < size - 1; k++) {
Arr[k] = Arr[k + 1];
}
size--; // Reduce array size
j--; // Check the new element at position j
}
}
}
printf("\nArray after removing duplicates: ");
for(i = 0; i < size; i++)
printf("%d\t", Arr[i]);
return 0;
}
- Accept an array of
Nintegers from the user. - Identify and remove duplicate numbers from the array.
- Print the array before and after removing duplicates.
Sample Input & Output
Enter the size of the array: 6 Enter array elements one by one: 55 44 55 66 55 77 Original Array: 55 44 55 66 55 77 Array after removing duplicates: 55 44 66 77
Code Explanation for duplicate number
- Input Array
- User enters the size
sizeand thensizeintegers intoArr[].
- User enters the size
- Print Original Array
- Simple loop prints all array elements.
- Duplicate Removal Logic
for(i = 0; i < size; i++) { for(j = i + 1; j < size; j++) { if(Arr[i] == Arr[j]) { for(k = j; k < size - 1; k++) Arr[k] = Arr[k + 1]; // Shift left size--; // Reduce array size j--; // Check current index again } } }- Outer loop picks each element
Arr[i]. - Inner loop compares it with all following elements.
- If a duplicate is found, shift elements left and reduce
size. j--ensures we check the new element at positionj.
- Outer loop picks each element
- Print Resulting Array
- Loop prints the array after duplicates are removed.
Real Time use Duplicate number
Database Management
- Unique IDs: In databases, duplicate IDs (like student ID, employee ID, or product codes) must be avoided.
- Data Cleaning: Detect and remove duplicate entries to ensure accuracy in reports and analytics.
Financial Systems
- Transactions: Prevent duplicate transaction numbers in banking or payment systems to avoid double billing.
- Invoice Management: Ensure invoices are unique to prevent errors in accounting.
E-Commerce and Inventory
- Product Codes: Remove duplicate SKUs or barcodes to maintain correct inventory records.
- Order Tracking: Avoid duplicate order numbers to prevent shipment confusion.
IoT & Embedded Systems
- Sensor Data: When collecting data from multiple sensors (temperature, vibration, etc.), duplicates can skew results. Removing duplicates ensures accurate real-time monitoring.
- Event Logging: In systems like security alarms, the system removes duplicate event IDs to prevent repeated alerts.
Software & Web Applications
- User Registration: Prevent multiple accounts with the same email or username.
- Form Submissions: Avoid duplicate entries when users accidentally submit the same form multiple times.
Data Analysis & Reporting
- Statistical Accuracy: Duplicate numbers in datasets can distort averages, totals, or trends.
- Machine Learning: Unique data points are crucial to train models correctly.
Previous & Next
- Previous : C Examples Bitwise Operators (System Concepts)
- Next : C Examples Fibonacci Sequence