Print all distinct strings from a given array
Last Updated :
18 Jun, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void findDisStr(vector<string>& arr, int N)
{
unordered_set<string> DistString;
for ( int i = 0; i < N; i++) {
if (!DistString.count(arr[i])) {
DistString.insert(arr[i]);
}
}
for ( auto String : DistString) {
cout << String << " " ;
}
}
int main()
{
vector<string> arr = { "Geeks" , "For" , "Geeks" ,
"Code" , "Coder" };
int N = arr.size();
findDisStr(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void findDisStr(List<String> arr, int N)
{
Set<String> DistString = new HashSet<String>();
for ( int i = 0 ; i < N; i++)
{
if (!DistString.contains(arr.get(i)))
{
DistString.add(arr.get(i));
}
}
for (String string : DistString)
{
System.out.print(string + " " );
}
}
public static void main(String[] args)
{
List<String> arr = Arrays.asList( new String[]{
"Geeks" , "For" , "Geeks" , "Code" , "Coder" });
int N = arr.size();
findDisStr(arr, N);
}
}
|
Python3
def findDisStr(arr, N):
DistString = set ()
for i in range (N):
if (arr[i] not in DistString):
DistString.add(arr[i])
for string in DistString:
print (string, end = " " )
if __name__ = = "__main__" :
arr = [ "Geeks" , "For" , "Geeks" ,
"Code" , "Coder" ]
N = len (arr)
findDisStr(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findDisStr(List< string > arr, int N)
{
HashSet< string > DistString = new HashSet< string >();
for ( int i = 0; i < N; i++)
{
if (!DistString.Contains(arr[i]))
{
DistString.Add(arr[i]);
}
}
foreach ( string a in DistString)
{
Console.Write(a + " " );
}
}
public static void Main(String[] args)
{
List<String> arr = new List< string >( new []{
"Geeks" , "For" , "Geeks" , "Code" , "Coder" });
int N = arr.Count;
findDisStr(arr, N);
}
}
|
Javascript
<script>
function findDisStr(arr, N) {
let DistString = new Set();
for (let i = N - 1; i >= 0; i--) {
if (!DistString.has(arr[i])) {
DistString.add(arr[i]);
}
}
for (let String of DistString) {
document.write(String + " " );
}
}
let arr = [ "Geeks" , "For" , "Geeks" ,
"Code" , "Coder" ];
let N = arr.length;
findDisStr(arr, N);
</script>
|
Output:
Coder Code Geeks For
Time Complexity: O(N * M), where M is the length of the longest string.
Auxiliary Space: O(N * M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...