print duplicate string Algorithm
The print duplicate string algorithm is a technique used to identify and display the duplicate characters or substrings within a given string or text. This algorithm is particularly useful in various text processing applications, such as data analysis, data cleaning, and natural language processing, where identifying the duplicate or repetitive patterns is essential. The algorithm typically iterates through the input string and maintains a data structure (such as a hash table, dictionary, or set) to store the unique characters or substrings encountered so far. During the iteration, the algorithm checks if the current character or substring already exists in the data structure. If it does, it means that the character or substring is a duplicate and can be printed or stored for further processing. If not, the character or substring is added to the data structure to keep track of the unique elements encountered.
There are several ways to implement the print duplicate string algorithm, with different trade-offs in terms of time and space complexity. A naive approach would be to use nested loops to compare each character or substring with every other character or substring in the input string. However, this method has a high time complexity of O(n^2), making it inefficient for large strings. A more efficient approach would be to use a data structure like a hash table or a set, which allows for faster look-up and insertion operations with an average time complexity of O(1). This way, the algorithm can iterate through the input string in linear time, resulting in an overall time complexity of O(n). However, this improved efficiency comes at the cost of additional space complexity, as the data structure used for tracking unique elements requires extra memory.
//
// C++ program to count all duplicates from string using hashing
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/strings
// https://github.com/allalgorithms/cpp
//
// Contributed by: Tushar Kanakagiri
// Github: @tusharkanakagiri
//
#include <stdio.h>
#include <stdlib.h>
#define NO_OF_CHARS 256
/* Fills count array with frequency of characters */
void fillCharCounts(char *str, int *count)
{
int i;
for (i = 0; *(str + i); i++)
count[*(str + i)]++;
}
/* Print duplicates present in the passed string */
void printDups(char *str)
{
// Create an array of size 256 and fill count of every character in it
int *count = (int *)calloc(NO_OF_CHARS, sizeof(int));
fillCharCounts(str, count);
// Print characters having count more than 0
int i;
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i] > 1)
printf("%c, count = %d \n", i, count[i]);
free(count);
}
/* Driver program to test to pront printDups*/
int main()
{
char str[] = ""; //Enter string here
printDups(str);
getchar();
return 0;
}