
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Duplicate in a Stream of Strings
The stream of strings is a sequential flow of given data where an individual element of the stream represents a string. The stream allows a large process of string data In C++, we have some STL(Standard template library) based functions such as count() and insert() to Check duplicate in a stream of strings.
Using Standard Template Library
The program uses C++ STL to set the header file unordered representing the unique key element and helps to solve the duplicates in a stream of strings.
Syntax
The following syntax is used in the examples-
static unordered_set<data_type> unq_string;
The unq_string is the declaration of a variable of the static datatype that means the variable and member function cannot be modified once it is declared. The unordered_set is a container data structure that sets only unique element.
count()
The count() is a predefined function of C++ STL that returns the element appearance of the given array string.
insert()
The insert() is another predefined function of C++ STL that can be used add the element before the specific element.
Algorithm
The following steps are-
Step 1: Start the program by mentioning the necessary header files such as iostream, unordered_set, and string.
Step 2: Then use the function definition check_dup_element() that accepts the parameter s of type const string& to work on the operation of string duplication.
Step 3: Initialize the container data structure "static unordered_set<data_type> unq_string;" which handles the main functionality of the code.
Step 4: Next, Using if-statement it set the condition based on repeated string in the given string i.e. variable s. If the repeated string found it will print the statement as "The repeated element found" otherwise it will use the built-in function insert() with static variable i.e. unq_string that only add the unique element present in the string and print the statement as "The unique element found".
Step 5: Now start the main function and store the array element in the variable str of type string. Then find the size of array to store in the variable n.
Step 6: Moving ahead to use the for loop that uses calling function named check_dup_element() that accept the parameter as str[i], where variable i iterates through the input string and display the result.
Example
In the following example, we will use the static variable
#include <iostream> #include <unordered_set> #include <string> using namespace std; void check_dup_element(const string& s) { static unordered_set<string> unq_string; // Check if the string is already present in the set if (unq_string.count(s) > 0) { cout << "The repeated element found" << endl; } else { unq_string.insert(s); cout << "The unique element found" << endl; } } // Start the main function int main() { string str[] = {"A", "B", "C", "C","A","D"}; // Find the size of an array int n = sizeof(str) / sizeof(str[0]); // Iteration of each element from the string for (int i = 0; i < n; i++) { check_dup_element(str[i]); } return 0; }
Output
The unique element found The unique element found The unique element found The repeated element found The repeated element found The unique element found
Conclusion
We explored the concept of stream duplication of string to find the repeated element from the given string array. This program used some pre-existing built-in functions such as count() and insert() to species the element appearance and inserting respectively. There are various application used such as data processing, natural language processing, log analysis, etc.