Check duplicates in a stream of strings

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
Yes



Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 time
void 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 code
int main()
{
    string arr[] = { "geeks", "for", "geeks" };
    int n = sizeof(arr) / sizeof(string);
  
    insertNames(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to insert the names
// and check whether they appear
// for the first time
static 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 code
public static void main(String[] args)
{
    String arr[] = { "geeks", "for", "geeks" };
    int n = arr.length;
  
    insertNames(arr, n);
}
}
  
// This code contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to insert the names 
# and check whether they appear 
# for the first time 
def 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 code 
if __name__ == "__main__"
  
    arr = [ "geeks", "for", "geeks" ]; 
    n = len(arr); 
  
    insertNames(arr, n); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// Function to insert the names
// and check whether they appear
// for the first time
static 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 code
public static void Main(String[] args)
{
    String []arr = { "geeks", "for", "geeks" };
    int n = arr.Length;
  
    insertNames(arr, n);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

No
No
Yes


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.