Check duplicates in a stream of strings - GeeksforGeeks

25 May, 2021
Given an array arr[] of strings containing the names of employees in a company. Assuming that the names are being entered in a system one after another, the task is to check whether the current name is entered for the first time or not.Examples:  
Input: arr[] = {“geeks”, “for”, “geeks”} Output: No No YesInput: arr[] = {“abc”, “aaa”, “cba”} Output: No No No  
 
Approach: Create an unordered_set to store the names of the employees and start traversing the array, if the current name is already present in the set then print Yes else print No and insert it into the set.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to insert the names// and check whether they appear// for the first timevoid insertNames(string arr[], int n){     // To store the names    // of the employees    unordered_set<string> set;    for (int i = 0; i < n; i++) {         // If current name is appearing        // for the first time        if (set.find(arr[i]) == set.end()) {            cout << "No\n";            set.insert(arr[i]);        }        else {            cout << "Yes\n";        }    }} // Driver codeint main(){    string arr[] = { "geeks", "for", "geeks" };    int n = sizeof(arr) / sizeof(string);     insertNames(arr, n);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function to insert the names// and check whether they appear// for the first timestatic void insertNames(String arr[], int n){     // To store the names    // of the employees    HashSet<String> set = new HashSet<String>();    for (int i = 0; i < n; i++)    {         // If current name is appearing        // for the first time        if (!set.contains(arr[i]))        {            System.out.print("No\n");            set.add(arr[i]);        }        else        {            System.out.print("Yes\n");        }    }} // Driver codepublic static void main(String[] args){    String arr[] = { "geeks", "for", "geeks" };    int n = arr.length;     insertNames(arr, n);}} // This code contributed by PrinciRaj1992
# Python3 implementation of the approach # Function to insert the names# and check whether they appear# for the first timedef insertNames(arr, n) :     # To store the names    # of the employees    string = set();         for i in range(n) :         # If current name is appearing        # for the first time        if arr[i] not in string :            print("No");            string.add(arr[i]);             else :            print("Yes");     # Driver codeif __name__ == "__main__" :     arr = [ "geeks", "for", "geeks" ];    n = len(arr);     insertNames(arr, n); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System;using System.Collections.Generic; class GFG{ // Function to insert the names// and check whether they appear// for the first timestatic void insertNames(String []arr, int n){     // To store the names    // of the employees    HashSet<String> set = new HashSet<String>();    for (int i = 0; i < n; i++)    {         // If current name is appearing        // for the first time        if (!set.Contains(arr[i]))        {            Console.Write("No\n");            set.Add(arr[i]);        }        else        {            Console.Write("Yes\n");        }    }} // Driver codepublic static void Main(String[] args){    String []arr = { "geeks", "for", "geeks" };    int n = arr.Length;     insertNames(arr, n);}} // This code is contributed by Rajput-Ji
<script>     // Javascript implementation of the approach     // Function to insert the names// and check whether they appear// for the first timefunction insertNames(arr, n){       // To store the names    // of the employees    let set = new Set();    for (let i = 0; i < n; i++)    {           // If current name is appearing        // for the first time        if (!set.has(arr[i]))        {            document.write("No"  + "<br/>");            set.add(arr[i]);        }        else        {            document.write("Yes" + "<br/>");        }    }}         // Driver code         let arr = [ "geeks", "for", "geeks" ];    let n = arr.length;       insertNames(arr, n);   // This code is contributed by susmitakundugoaldanga.</script>
No
No
Yes
 
ankthon
princiraj1992
Rajput-Ji
susmitakundugoaldanga
Hash
Searching
Strings
Searching
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Rearrange an array such that arr[i] = i
Quadratic Probing in Hashing
Hashing in Java
Non-Repeating Element
What are Hash Functions and How to choose a good Hash Function?
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Find the Missing Number
K'th Smallest/Largest Element in Unsorted Array | Set 1