# Print all distinct strings from a given array

Given an array of strings arr[] of size N, the task is to print all the distinct strings present in the given array.

Examples:

Input: arr[] = { “Geeks”, “For”, “Geeks”, “Code”, “Coder” }
Output: Coder Code Geeks For
Explanation: Since all the strings in the array are distinct, the required output is Coder Code Geeks For.

Input: arr[] = { “Good”, “God”, “Good”, “God”, “god” }
Output: god Good God

Naive Approach: The simplest approach to solve this problem is to sort the array based on the lexicographical order of the strings. Traverse the array and check if the current string of the array is equal to the previously traversed string or not. If found to be false, then print the current string.

Time Complexity: O(N * M * log(N)), where M is the length of the longest string.
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use Hashing. Follow the steps below to solve the problem:

• Initialize a Set, say DistString, to store the distinct strings from the given array.
• Traverse the array and insert array elements into DistString.
• Finally, print all strings from DistString.

Below is the implementation of the above approach.

## C++

 `// C++ program to implement` `// the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the distinct strings` `// from the given array` `void` `findDisStr(vector& arr, ``int` `N)` `{` `    ``// Stores distinct strings` `    ``// from the given array` `    ``unordered_set DistString;`   `    ``// Traverse the array` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// If current string not` `        ``// present into the set` `        ``if` `(!DistString.count(arr[i])) {`   `            ``// Insert current string` `            ``// into the set` `            ``DistString.insert(arr[i]);` `        ``}` `    ``}`   `    ``// Traverse the set DistString` `    ``for` `(``auto` `String : DistString) {`   `        ``// Print distinct string` `        ``cout << String << ``" "``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``vector arr = { ``"Geeks"``, ``"For"``, ``"Geeks"``,` `                           ``"Code"``, ``"Coder"` `};`   `    ``// Stores length of the array` `    ``int` `N = arr.size();`   `    ``findDisStr(arr, N);` `    ``return` `0;` `}`

## Java

 `// Java program to implement` `// the above approach` `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG{`   `// Function to find the distinct strings` `// from the given array` `static` `void` `findDisStr(List arr, ``int` `N)` `{` `    `  `    ``// Stores distinct strings` `    ``// from the given array` `    ``Set DistString = ``new` `HashSet();`   `    ``// Traverse the array` `    ``for``(``int` `i = ``0``; i < N; i++)` `    ``{` `        `  `        ``// If current string not` `        ``// present into the set` `        ``if` `(!DistString.contains(arr.get(i)))` `        ``{` `            `  `            ``// Insert current string` `            ``// into the set` `            ``DistString.add(arr.get(i));` `        ``}` `    ``}`   `    ``// Traverse the set DistString` `    ``for``(String string : DistString) ` `    ``{` `        `  `        ``// Print distinct string` `        ``System.out.print(string + ``" "``);` `    ``}` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``List arr = Arrays.asList(``new` `String[]{` `        ``"Geeks"``, ``"For"``, ``"Geeks"``, ``"Code"``, ``"Coder"` `});`   `    ``// Stores length of the array` `    ``int` `N = arr.size();`   `    ``findDisStr(arr, N);` `}` `}`   `// This code is contributed by jithin`

## Python3

 `# Python3 program to implement` `# the above approach`   `# Function to find the distinct ` `# strings from the given array` `def` `findDisStr(arr, N):` `    `  `    ``# Stores distinct strings` `    ``# from the given array` `    ``DistString ``=` `set``()`   `    ``# Traverse the array` `    ``for` `i ``in` `range``(N):`   `        ``# If current string not` `        ``# present into the set` `        ``if` `(arr[i] ``not` `in` `DistString):`   `            ``# Insert current string` `            ``# into the set` `            ``DistString.add(arr[i])`   `    ``# Traverse the set DistString` `    ``for` `string ``in` `DistString:` `        `  `        ``# Print distinct string` `        ``print``(string, end ``=` `" "``)` `   `  `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `  `  `    ``arr ``=` `[ ``"Geeks"``, ``"For"``, ``"Geeks"``,` `            ``"Code"``, ``"Coder"` `]`   `    ``# Stores length of the array` `    ``N ``=` `len``(arr)`   `    ``findDisStr(arr, N)` `    `  `# This code is contributed by chitranayal`

## C#

 `// C# program to implement` `// the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to find the distinct strings` `// from the given array` `static` `void` `findDisStr(List<``string``> arr, ``int` `N)` `{` `    `  `    ``// Stores distinct strings` `    ``// from the given array` `    ``HashSet<``string``> DistString = ``new` `HashSet<``string``>();`   `    ``// Traverse the array` `    ``for``(``int` `i = 0; i < N; i++)` `    ``{` `        `  `        ``// If current string not` `        ``// present into the set` `        ``if` `(!DistString.Contains(arr[i]))` `        ``{` `            `  `            ``// Insert current string` `            ``// into the set` `            ``DistString.Add(arr[i]);` `        ``}` `    ``}`   `    ``// Traverse the set DistString` `    ``foreach``(``string` `a ``in` `DistString)` `    ``{  ` `      ``Console.Write(a +``" "``);  ` `    ``}  ` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``List arr = ``new` `List<``string``>(``new` `[]{` `        ``"Geeks"``, ``"For"``, ``"Geeks"``, ``"Code"``, ``"Coder"` `});`   `    ``// Stores length of the array` `    ``int` `N = arr.Count;`   `    ``findDisStr(arr, N);` `}` `}`   `// This code is contributed by jana_sayantan`

## Javascript

 ``

Output:

`Coder Code Geeks For`

Time Complexity: O(N * M), where M is the length of the longest string.
Auxiliary Space: O(N * M)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next