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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the distinct strings
// from the given array
void findDisStr(vector<string>& arr, int N)
{
// Stores distinct strings
// from the given array
unordered_set<string> 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<string> 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<String> arr, int N)
{
// Stores distinct strings
// from the given array
Set<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.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<String> 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<String> 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
<script>
// JavaScript program to implement
// the above approach
// Function to find the distinct strings
// from the given array
function findDisStr(arr, N) {
// Stores distinct strings
// from the given array
let DistString = new Set();
// Traverse the array
for (let i = N - 1; i >= 0; i--) {
// If current string not
// present into the set
if (!DistString.has(arr[i])) {
// Insert current string
// into the set
DistString.add(arr[i]);
}
}
for (let String of DistString) {
// Print distinct string
document.write(String + " ");
}
}
// Driver Code
let arr = ["Geeks", "For", "Geeks",
"Code", "Coder"];
// Stores length of the array
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)
Similar Reads
Print all Distinct (Unique) Elements in given Array Given an integer array arr[], print all distinct elements from this array. The given array may contain duplicates and the output should contain every element only once.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: {12, 10, 9, 45, 2}Input: arr[] = {1, 2, 3, 4, 5}Output: {1, 2, 3, 4,
11 min read
Print all Unique Strings present in a given Array Given an array of strings arr[], the task is to print all unique strings that are present in the given array. Examples: Input: arr[] = { "geeks", "geek", "ab", "geek" "code", "karega" } Output: geeks ab code karega Explanation: The frequency of the string "geeks" is 1. The frequency of the string "g
15+ min read
Print All Distinct Permutations of an Array Given an array arr[], Print all distinct permutations of the given array.Examples:Input: arr[] = [1, 3, 3]Output: [[1, 3, 3], [3, 1, 3], [3, 3, 1]]Explanation: Above are all distinct permutations of given array.Input: arr[] = [2, 2] Output: [[2, 2]]Explanation: Above are all distinct permutations of
6 min read
Print all distinct characters of a string in order (3 Methods) Given a string, find the all distinct (or non-repeating characters) in it. For example, if the input string is âGeeks for Geeksâ, then output should be 'for' and if input string is âGeeks Quizâ, then output should be âGksQuizâ.The distinct characters should be printed in same order as they appear in
14 min read
Print all the duplicate characters in a string Given a string s, the task is to identify all characters that appear more than once and print each as a list containing the character and its count. Examples:Input: s = "geeksforgeeks"Output: ['e', 4], ['g', 2], ['k', 2], ['s', 2]Explanation: Characters e, g, k, and s appear more than once. Their co
8 min read