Find the distinct elements in a given array

Here given code implementation process.

// C program 
// Find the distinct elements in a given array
#include <stdio.h>

//Function which is display array elements
void display(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("   %d", arr[i]);
	}
	printf("\n");
}
// Display of all distinct elements in given array
void distinctElement(int arr[], int size)
{
	display(arr, size);
	// Loop controlling variables
	int i = 0;
	int j = 0;
	// distinct element indicator
	int status;
	int counter = 0;
	printf(" Distinct element are :");
	// iterating through the size
	for (i = 0; i < size; ++i)
	{
		status = 1;
		// Inner loop test distinct element
		for (j = 0; j < i && status == 1; ++j)
		{
			if (arr[i] == arr[j])
			{
				// When elements is repeated
				status = 0;
			}
		}
		if (status == 1)
		{
			// distinct element
			printf("   %d", arr[i]);
			counter = 1;
		}
	}
	if (counter == 0)
	{
		printf("None");
	}
	printf("\n");
}
int main()
{
	// Define array of integer elements
	int arr1[] = {
		7 , 1 , 4 , 9 , 1 , 3 , 8 , 6 , 2 , 4 , 6 , 2 , 5 , 7
	};
	int arr2[] = {
		1 , 2 , 3 , 9 , 1 , 4 , 3
	};
	int size = sizeof(arr1) / sizeof(arr1[0]);
	distinctElement(arr1, size);
	size = sizeof(arr2) / sizeof(arr2[0]);
	distinctElement(arr2, size);
	return 0;
}

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
/*
   Java Program
   Find the distinct elements in a given array
*/
public class SearchDistinct
{
	//Function which is display array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("   " + arr[i]);
		}
		System.out.print("\n");
	}
	// Display of all distinct elements in given array
	public void distinctElement(int[] arr, int size)
	{
		display(arr, size);
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// distinct element indicator
		boolean status = true;
		boolean result = false;
		System.out.print(" Distinct element are :");
		// iterating through the size
		for (i = 0; i < size; ++i)
		{
			// Inner loop test distinct element
			for (j = 0; j < i && status == true; ++j)
			{
				if (arr[i] == arr[j])
				{
					// When elements is repeated
					status = false;
				}
			}
			if (status == true)
			{
				// distinct element
				System.out.print("   " + arr[i] );
				result = true;
			}
			status = true;
		}
		if (result == false)
		{
			System.out.print("None");
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		SearchDistinct task = new SearchDistinct();
		// Define array of integer elements
		int[] arr1 = {
			7 , 1 , 4 , 9 , 1 , 3 , 8 , 6 , 2 , 4 , 6 , 2 , 5 , 7
		};
		int[] arr2 = {
			1 , 2 , 3 , 9 , 1 , 4 , 3
		};
		int size = arr1.length;
		task.distinctElement(arr1, size);
		size = arr2.length;
		task.distinctElement(arr2, size);
	}
}

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
// Include header file
#include <iostream>

using namespace std;
/*
   C++ Program
   Find the distinct elements in a given array
*/
class SearchDistinct
{
	public:
		//Function which is display array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "   " << arr[i];
			}
			cout << "\n";
		}
	// Display of all distinct elements in given array
	void distinctElement(int arr[], int size)
	{
		this->display(arr, size);
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// distinct element indicator
		bool status = true;
		bool result = false;
		cout << " Distinct element are :";
		// iterating through the size
		for (i = 0; i < size; ++i)
		{
			// Inner loop test distinct element
			for (j = 0; j < i && status == true; ++j)
			{
				if (arr[i] == arr[j])
				{
					// When elements is repeated
					status = false;
				}
			}
			if (status == true)
			{
				// distinct element
				cout << "   " << arr[i];
				result = true;
			}
			status = true;
		}
		if (result == false)
		{
			cout << "None";
		}
		cout << "\n";
	}
};
int main()
{
	SearchDistinct task = SearchDistinct();
	// Define array of integer elements
	int arr1[] = {
		7 , 1 , 4 , 9 , 1 , 3 , 8 , 6 , 2 , 4 , 6 , 2 , 5 , 7
	};
	int arr2[] = {
		1 , 2 , 3 , 9 , 1 , 4 , 3
	};
	int size = sizeof(arr1) / sizeof(arr1[0]);
	task.distinctElement(arr1, size);
	size = sizeof(arr2) / sizeof(arr2[0]);
	task.distinctElement(arr2, size);
	return 0;
}

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
// Include namespace system
using System;
/*
   C# Program
   Find the distinct elements in a given array
*/
public class SearchDistinct
{
	//Function which is display array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("   " + arr[i]);
		}
		Console.Write("\n");
	}
	// Display of all distinct elements in given array
	public void distinctElement(int[] arr, int size)
	{
		display(arr, size);
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// distinct element indicator
		Boolean status = true;
		Boolean result = false;
		Console.Write(" Distinct element are :");
		// iterating through the size
		for (i = 0; i < size; ++i)
		{
			// Inner loop test distinct element
			for (j = 0; j < i && status == true; ++j)
			{
				if (arr[i] == arr[j])
				{
					// When elements is repeated
					status = false;
				}
			}
			if (status == true)
			{
				// distinct element
				Console.Write("   " + arr[i]);
				result = true;
			}
			status = true;
		}
		if (result == false)
		{
			Console.Write("None");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		SearchDistinct task = new SearchDistinct();
		// Define array of integer elements
		int[] arr1 = {
			7 , 1 , 4 , 9 , 1 , 3 , 8 , 6 , 2 , 4 , 6 , 2 , 5 , 7
		};
		int[] arr2 = {
			1 , 2 , 3 , 9 , 1 , 4 , 3
		};
		int size = arr1.Length;
		task.distinctElement(arr1, size);
		size = arr2.Length;
		task.distinctElement(arr2, size);
	}
}

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
<?php
/*
   Php Program
   Find the distinct elements in a given array
*/
class SearchDistinct
{
	//Function which is display array elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "   ". $arr[$i];
		}
		echo "\n";
	}
	// Display of all distinct elements in given array
	public	function distinctElement( & $arr, $size)
	{
		$this->display($arr, $size);
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// distinct element indicator
		$status = true;
		$result = false;
		echo " Distinct element are :";
		// iterating through the size
		for ($i = 0; $i < $size; ++$i)
		{
			// Inner loop test distinct element
			for ($j = 0; $j < $i && $status == true; ++$j)
			{
				if ($arr[$i] == $arr[$j])
				{
					// When elements is repeated
					$status = false;
				}
			}
			if ($status == true)
			{
				// distinct element
				echo "   ". $arr[$i];
				$result = true;
			}
			$status = true;
		}
		if ($result == false)
		{
			echo "None";
		}
		echo "\n";
	}
}

function main()
{
	$task = new SearchDistinct();
	// Define array of integer elements
	$arr1 = array(7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7);
	$arr2 = array(1, 2, 3, 9, 1, 4, 3);
	$size = count($arr1);
	$task->distinctElement($arr1, $size);
	$size = count($arr2);
	$task->distinctElement($arr2, $size);
}
main();

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
/*
   Node Js Program
   Find the distinct elements in a given array
*/
class SearchDistinct
{
	//Function which is display array elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("   " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Display of all distinct elements in given array
	distinctElement(arr, size)
	{
		this.display(arr, size);
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// distinct element indicator
		var status = true;
		var result = false;
		process.stdout.write(" Distinct element are :");
		// iterating through the size
		for (i = 0; i < size; ++i)
		{
			// Inner loop test distinct element
			for (j = 0; j < i && status == true; ++j)
			{
				if (arr[i] == arr[j])
				{
					// When elements is repeated
					status = false;
				}
			}
			if (status == true)
			{
				// distinct element
				process.stdout.write("   " + arr[i]);
				result = true;
			}
			status = true;
		}
		if (result == false)
		{
			process.stdout.write("None");
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new SearchDistinct();
	// Define array of integer elements
	var arr1 = [7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7];
	var arr2 = [1, 2, 3, 9, 1, 4, 3];
	var size = arr1.length;
	task.distinctElement(arr1, size);
	size = arr2.length;
	task.distinctElement(arr2, size);
}
main();

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
#  Python 3 Program
#  Find the distinct elements in a given array

class SearchDistinct :
	# Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print("   ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Display of all distinct elements in given list 
	def distinctElement(self, arr, size) :
		self.display(arr, size)
		#  Loop controlling variables
		i = 0
		j = 0
		#  distinct element indicator
		status = True
		result = False
		print(" Distinct element are :", end = "")
		#  iterating through the size
		while (i < size) :
			#  Inner loop test distinct element
			while (j < i and status == True) :
				if (arr[i] == arr[j]) :
					#  When elements is repeated
					status = False
				
				j += 1
			
			if (status == True) :
				#  distinct element
				print("   ", arr[i], end = "")
				result = True
			
			status = True
			j = 0
			i += 1
		
		if (result == False) :
			print("None", end = "")
		
		print(end = "\n")
	

def main() :
	task = SearchDistinct()
	#  Define list (array) of integer elements
	arr1 = [7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7]
	arr2 = [1, 2, 3, 9, 1, 4, 3]
	size = len(arr1)
	task.distinctElement(arr1, size)
	size = len(arr2)
	task.distinctElement(arr2, size)

if __name__ == "__main__": main()

Output

    7    1    4    9    1    3    8    6    2    4    6    2    5    7
 Distinct element are :    7    1    4    9    3    8    6    2    5
    1    2    3    9    1    4    3
 Distinct element are :    1    2    3    9    4
#    Ruby Program
#    Find the distinct elements in a given array

class SearchDistinct 
	# Function which is display array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print("   ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Display of all distinct elements in given array
	def distinctElement(arr, size) 
		self.display(arr, size)
		#  Loop controlling variables
		i = 0
		j = 0
		#  distinct element indicator
		status = true
		result = false
		print(" Distinct element are :")
		#  iterating through the size
		while (i < size) 
			#  Inner loop test distinct element
			while (j < i && status == true) 
				if (arr[i] == arr[j]) 
					#  When elements is repeated
					status = false
				end

				j += 1
			end

			if (status == true) 
				#  distinct element
				print("   ", arr[i])
				result = true
			end

			status = true
			j = 0
			i += 1
		end

		if (result == false) 
			print("None")
		end

		print("\n")
	end

end

def main() 
	task = SearchDistinct.new()
	#  Define array of integer elements
	arr1 = [7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7]
	arr2 = [1, 2, 3, 9, 1, 4, 3]
	size = arr1.length
	task.distinctElement(arr1, size)
	size = arr2.length
	task.distinctElement(arr2, size)
end

main()

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
/*
   Scala Program
   Find the distinct elements in a given array
*/
class SearchDistinct
{
	//Function which is display array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("   " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Display of all distinct elements in given array
	def distinctElement(arr: Array[Int], size: Int): Unit = {
		this.display(arr, size);
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// distinct element indicator
		var status: Boolean = true;
		var result: Boolean = false;
		print(" Distinct element are :");
		// iterating through the size
		while (i < size)
		{
			// Inner loop test distinct element
			while (j < i && status == true)
			{
				if (arr(i) == arr(j))
				{
					// When elements is repeated
					status = false;
				}
				j += 1;
			}
			if (status == true)
			{
				// distinct element
				print("   " + arr(i));
				result = true;
			}
			status = true;
			j = 0;
			i += 1;
		}
		if (result == false)
		{
			print("None");
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SearchDistinct = new SearchDistinct();
		// Define array of integer elements
		var arr1: Array[Int] = Array(7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7);
		var arr2: Array[Int] = Array(1, 2, 3, 9, 1, 4, 3);
		var size: Int = arr1.length;
		task.distinctElement(arr1, size);
		size = arr2.length;
		task.distinctElement(arr2, size);
	}
}

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4
/*
   Swift 4 Program
   Find the distinct elements in a given array
*/
class SearchDistinct
{
	//Function which is display array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("   ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Display of all distinct elements in given array
	func distinctElement(_ arr: [Int], _ size: Int)
	{
		self.display(arr, size);
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// distinct element indicator
		var status: Bool = true;
		var result: Bool = false;
		print(" Distinct element are :", terminator: "");
		// iterating through the size
		while (i < size)
		{
			// Inner loop test distinct element
			while (j < i && status == true)
			{
				if (arr[i] == arr[j])
				{
					// When elements is repeated
					status = false;
				}
				j += 1;
			}
			if (status == true)
			{
				// distinct element
				print("   ", arr[i], terminator: "");
				result = true;
			}
			status = true;
			j = 0;
			i += 1;
		}
		if (result == false)
		{
			print("None", terminator: "");
		}
		print(terminator: "\n");
	}
}
func main()
{
	let task: SearchDistinct = SearchDistinct();
	// Define array of integer elements
	let arr1: [Int] = [7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7];
	let arr2: [Int] = [1, 2, 3, 9, 1, 4, 3];
	var size: Int = arr1.count;
	task.distinctElement(arr1, size);
	size = arr2.count;
	task.distinctElement(arr2, size);
}
main();

Output

    7    1    4    9    1    3    8    6    2    4    6    2    5    7
 Distinct element are :    7    1    4    9    3    8    6    2    5
    1    2    3    9    1    4    3
 Distinct element are :    1    2    3    9    4
/*
   Kotlin Program
   Find the distinct elements in a given array
*/
class SearchDistinct
{
	//Function which is display array elements
	fun display(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("   " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Display of all distinct elements in given array
	fun distinctElement(arr: Array<Int>, size: Int): Unit
	{
		this.display(arr, size);
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// distinct element indicator
		var status: Boolean = true;
		var result: Boolean = false;
		print(" Distinct element are :");
		// iterating through the size
		while (i<size)
		{
			// Inner loop test distinct element
			while (j<i && status == true)
			{
				if (arr[i] == arr[j])
				{
					// When elements is repeated
					status = false;
				}
				j += 1;
			}
			if (status == true)
			{
				// distinct element
				print("   " + arr[i]);
				result = true;
			}
			status = true;
			j = 0;
			i += 1;
		}
		if (result == false)
		{
			print("None");
		}
		print("\n");
	}
}
fun main(args: Array<String>): Unit
{
	var task: SearchDistinct = SearchDistinct();
	// Define array of integer elements
	var arr1: Array<Int> = arrayOf(7, 1, 4, 9, 1, 3, 8, 6, 2, 4, 6, 2, 5, 7);
	var arr2: Array<Int> = arrayOf(1, 2, 3, 9, 1, 4, 3);
	var size: Int = arr1.count();
	task.distinctElement(arr1, size);
	size = arr2.count();
	task.distinctElement(arr2, size);
}

Output

   7   1   4   9   1   3   8   6   2   4   6   2   5   7
 Distinct element are :   7   1   4   9   3   8   6   2   5
   1   2   3   9   1   4   3
 Distinct element are :   1   2   3   9   4


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