Find the distinct strings present in a given array

Here given code implementation process.

/*
    Java Program
    Find the distinct strings present in a given array
*/
import java.util.HashMap;
public class Distinct
{
    public void findDistinct(String[] arr, int n)
    {
        // Use to collect unique element 
        HashMap < String, Boolean > record = new HashMap < String, Boolean > ();
        // Collect unique element
        for (int i = 0; i < n; ++i)
        {
            if (record.containsKey(arr[i]) == false)
            {
                record.put(arr[i], true);
            }
            else
            {
                record.put(arr[i], false);
            }
        }
        // Display result
        for (String info: record.keySet())
        {
            if (record.get(info) )
            {
                System.out.print("  " + info);
            }
        }
    }
    public static void main(String[] args)
    {
        Distinct task = new Distinct();
        String[] arr = {
            "apple" , "orange" , "apple" , 
            "mango" , "cherry" , "grapes" , "cherry"
        };
        // Get the number of element
        int n = arr.length;
        task.findDistinct(arr, n);
    }
}

input

  orange  mango  grapes
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
class Distinct
{
	public: void findDistinct(string arr[], int n)
	{
		// Use to collect unique element 
		unordered_map < string, bool > record ;
		// Collect unique element
		for (int i = 0; i < n; ++i)
		{
			if (record.find(arr[i]) != record.end() == false)
			{
				record[arr[i]] = true;
			}
			else
			{
				record[arr[i]] = false;
			}
		}
		// Display result
		for (auto &info: record)
		{
			if (info.second)
			{
				cout << "  " << info.first;
			}
		}
	}
};
int main()
{
	Distinct *task = new Distinct();
	string arr[] = {
		"apple" , "orange" , "apple" , "mango" , 
        "cherry" , "grapes" , "cherry"
	};
	// Get the number of element
	int n = sizeof(arr) / sizeof(arr[0]);
	task->findDistinct(arr, n);
	return 0;
}

input

  grapes  orange  mango
// Include namespace system
using System;
using System.Collections.Generic;
public class Distinct
{
	public void findDistinct(string[] arr, int n)
	{
		// Use to collect unique element 
		Dictionary < string, bool > record = 
                   new Dictionary < string, bool > ();
		// Collect unique element
		for (int i = 0; i < n; ++i)
		{
			if (record.ContainsKey(arr[i]) == false)
			{
				record.Add(arr[i], true);
			}
			else
			{
				record[arr[i]] = false;
			}
		}
		// Display result
		foreach(KeyValuePair < string, bool > info in record)
		{
			if (info.Value)
			{
				Console.Write("  " + info.Key);
			}
		}
	}
	public static void Main(String[] args)
	{
		Distinct task = new Distinct();
		String[] arr = {
			"apple" , "orange" , "apple" , 
            "mango" , "cherry" , "grapes" , "cherry"
		};
		// Get the number of element
		int n = arr.Length;
		task.findDistinct(arr, n);
	}
}

input

  orange  mango  grapes
<?php
class Distinct
{
	public	function findDistinct($arr, $n)
	{
		// Use to collect unique element 
		$record = array();
		// Collect unique element
		for ($i = 0; $i < $n; ++$i)
		{
			if (array_key_exists($arr[$i], $record) == false)
			{
				$record[$arr[$i]] = true;
			}
			else
			{
				$record[$arr[$i]] = false;
			}
		}
		// Display result
		foreach($record as $key => $value)
		{
			if ($value)
			{
				echo("  ".$key);
			}
		}
	}
}

function main()
{
	$task = new Distinct();
	$arr = array("apple", "orange", "apple", 
                 "mango", "cherry", "grapes", "cherry");
	// Get the number of element
	$n = count($arr);
	$task->findDistinct($arr, $n);
}
main();

input

  orange  mango  grapes
class Distinct
{
	findDistinct(arr, n)
	{
		// Use to collect unique element 
		var record = new Map();
		// Collect unique element
		for (var i = 0; i < n; ++i)
		{
			if (record.has(arr[i]) == false)
			{
				record.set(arr[i], true);
			}
			else
			{
				record.set(arr[i], false);
			}
		}
		// Display result
		for (let [key, value] of record)
		{
			if (value)
			{
				process.stdout.write("  " + key);
			}
		}
	}
}

function main()
{
	var task = new Distinct();
	var arr = ["apple", "orange", "apple", 
               "mango", "cherry", "grapes", "cherry"];
	// Get the number of element
	var n = arr.length;
	task.findDistinct(arr, n);
}
main();

input

  orange  mango  grapes
#    Python 3 Program
#    Find the distinct strings present in a given array
class Distinct :
	def findDistinct(self, arr, n) :
		#  Use to collect unique element 
		record = dict()
		i = 0
		#  Collect unique element
		while (i < n) :
			if ((arr[i] in record.keys()) == False) :
				record[arr[i]] = True
			else :
				record[arr[i]] = False
			
			i += 1
		
		for key, value in record.items() :
			if (value) :
				print("  ", key, end = "")
			
		
	

def main() :
	task = Distinct()
	arr = ["apple", "orange", "apple", 
           "mango", "cherry", "grapes", "cherry"]
	#  Get the number of element
	n = len(arr)
	task.findDistinct(arr, n)

if __name__ == "__main__": main()

input

   grapes   mango   orange
#    Ruby Program
#    Find the distinct strings present in a given array
class Distinct 
	def findDistinct(arr, n) 
		#  Use to collect unique element 
		record = Hash.new()
		i = 0
		#  Collect unique element
		while (i < n) 
			if (record.key?(arr[i]) == false) 
				record[arr[i]] = true
			else
				record[arr[i]] = false
			end

			i += 1
		end

		#  Display result
		record.each { | key, value |
			if (value) 
				print("  ", key)
			end
		}
	end

end

def main() 
	task = Distinct.new()
	arr = ["apple", "orange", "apple", "mango", 
           "cherry", "grapes", "cherry"]
	#  Get the number of element
	n = arr.length
	task.findDistinct(arr, n)
end

main()

input

  orange  mango  grapes
import scala.collection.mutable._;
/*
    Scala Program
    Find the distinct strings present in a given array
*/
class Distinct()
{
	def findDistinct(arr: Array[String], n: Int): Unit = {
		// Use to collect unique element 
		var record: HashMap[String, Boolean] = 
                    new HashMap[String, Boolean]();
		var i: Int = 0;
		// Collect unique element
		while (i < n)
		{
			if (record.contains(arr(i)) == false)
			{
				record.addOne(arr(i), true);
			}
			else
			{
				record.addOne(arr(i), false);
			}
			i += 1;
		}
		// Display result
		for((key,value) <- record)
		{
			if (value)
			{
				print("  " + key);
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Distinct = new Distinct();
		var arr: Array[String] = Array(
          "apple", "orange", "apple", "mango", 
          "cherry", "grapes", "cherry");
		// Get the number of element
		var n: Int = arr.length;
		task.findDistinct(arr, n);
	}
}

input

  orange  grapes  mango
import Foundation;
/*
    Swift 4 Program
    Find the distinct strings present in a given array
*/
class Distinct
{
	func findDistinct(_ arr: [String], _ n: Int)
	{
		// Use to collect unique element 
		var record = [String : Bool]();
		var i = 0;
		// Collect unique element
		while (i < n)
		{
			if (record.keys.contains(arr[i]) == false)
			{
				record[arr[i]] = true;
			}
			else
			{
				record[arr[i]] = false;
			}
			i += 1;
		}
		// Display result
		for (key, value) in record
		{
			if (value)
			{
				print("  ", key, terminator: "");
			}
		}
	}
}
func main()
{
	let task = Distinct();
	let arr = [
      "apple", "orange", "apple", "mango", 
      "cherry", "grapes", "cherry"
    ];
	// Get the number of element
	let n = arr.count;
	task.findDistinct(arr, n);
}
main();

input

   orange   mango   grapes
/*
    Kotlin Program
    Find the distinct strings present in a given array
*/
class Distinct
{
	fun findDistinct(arr: Array < String > , n: Int): Unit
	{
		// Use to collect unique element 
		var record = mutableMapOf<String, Boolean>();
		var i: Int = 0;
		// Collect unique element
		while (i < n)
		{
			if (record.containsKey(arr[i]) == false)
			{
				record.put(arr[i], true);
			}
			else
			{
				record.put(arr[i], false);
			}
			i += 1;
		}
		// Display result
		for ((key, value) in record)
		{
			if (value)
			{
				print("  " + key);
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Distinct = Distinct();
	val arr: Array < String > = arrayOf(
      "apple", "orange", "apple", "mango", 
      "cherry", "grapes", "cherry");
  
	// Get the number of element
	val n: Int = arr.count();
	task.findDistinct(arr, n);
}

input

  orange  mango  grapes

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved