Find unique elements in sorted array

Here given code implementation process.

// C Program 
// Find unique elements in sorted array
#include <stdio.h>

#include <limits.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");
}
//Finding all unique elements in given sorted array
void unique_elements(int arr[], int size)
{
	if (size < 1)
	{
		return;
	}
	//Display array element
	printf("\n Array  :");
	display(arr, size);
	printf(" Unique elements : ");
	if (size == 1)
	{
		//When only one element in array
		printf(" %d\n", arr[0]);
		return;
	}
	int status = 0;
	for (int i = 0; i < size; ++i)
	{
		if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
		{
			//When first or last element are unique
			printf("  %d", arr[i]);
			status = 1;
		}
		else if (i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
		{
			//When Intermediate elements are unique
			printf("  %d", arr[i]);
			status = 1;
		}
	}
	printf("\n");
	if (status == 0)
	{
		printf("\n Not Found");
	}
}
int main()
{
	//Define array of integer elements
	int arr1[] = {
		1,
		1,
		4,
		6,
		7,
		7,
		9,
		9,
		9
	};
	//Get the size of array
	int size = sizeof(arr1) / sizeof(arr1[0]);
	unique_elements(arr1, size);
	int arr2[] = {
		-3,
		-2,
		2,
		3,
		4,
		4,
		4,
		5,
		5,
		6
	};
	//Get the size of array
	size = sizeof(arr2) / sizeof(arr2[0]);
	unique_elements(arr2, size);
	int arr3[] = {
		4
	};
	//Get the size of array
	size = sizeof(arr3) / sizeof(arr3[0]);
	unique_elements(arr3, size);
	return 0;
}

Output

 Array  :  1  1  4  6  7  7  9  9  9
 Unique elements :   4  6

 Array  :  -3  -2  2  3  4  4  4  5  5  6
 Unique elements :   -3  -2  2  3  6

 Array  :  4
 Unique elements :  4
/*
  Java Program
  Find unique elements in sorted array
*/
class MyArray
{
	//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");
	}
	//Finding all unique elements in given sorted array
	public void unique_elements(int[] arr, int size)
	{
		if (size < 1)
		{
			return;
		}
		System.out.print("\n Array :");
		display(arr, size);
		System.out.print(" Unique elements : ");
		if (size == 1)
		{
			System.out.print(" " + arr[0] + "\n");
			return;
		}
		int status = 0;
		for (int i = 0; i < size; ++i)
		{
			if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
			{
				System.out.print(" " + arr[i]);
				status = 1;
			}
			else if (i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
			{
				System.out.print(" " + arr[i]);
				status = 1;
			}
		}
		System.out.print("\n");
		if (status == 0)
		{
			System.out.print("\n Not Found");
		}
	}
	public static void main(String[] args)
	{
		MyArray obj = new MyArray();
		//Define array of integer elements
		int[] arr1 = {
			1,
			1,
			4,
			6,
			7,
			7,
			9,
			9,
			9
		};
		//Get the size of array
		int size = arr1.length;
		obj.unique_elements(arr1, size);
		int[] arr2 = {
			-3,
			-2,
			2,
			3,
			4,
			4,
			4,
			5,
			5,
			6
		};
		//Get the size of array
		size = arr2.length;
		obj.unique_elements(arr2, size);
		int[] arr3 = {
			4
		};
		//Get the size of array
		size = arr3.length;
		obj.unique_elements(arr3, size);
	}
}

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
//Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Find unique elements in sorted array
*/
class MyArray
{
	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";
		}
	//Finding all unique elements in given sorted array
	void unique_elements(int arr[], int size)
	{
		if (size < 1)
		{
			return;
		}
		cout << "\n Array :";
		this->display(arr, size);
		cout << " Unique elements : ";
		if (size == 1)
		{
			cout << " " << arr[0] << "\n";
			return;
		}
		int status = 0;
		for (int i = 0; i < size; ++i)
		{
			if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
			{
				cout << " " << arr[i];
				status = 1;
			}
			else if (i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
			{
				cout << " " << arr[i];
				status = 1;
			}
		}
		cout << "\n";
		if (status == 0)
		{
			cout << "\n Not Found";
		}
	}
};
int main()
{
	MyArray obj = MyArray();
	int arr1[] = {
		1 , 1 , 4 , 6 , 7 , 7 , 9 , 9 , 9
	};
	//Get the size of array
	int size = sizeof(arr1) / sizeof(arr1[0]);
	obj.unique_elements(arr1, size);
	int arr2[] = {
		-3 , -2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6
	};
	//Get the size of array
	size = sizeof(arr2) / sizeof(arr2[0]);
	obj.unique_elements(arr2, size);
	int arr3[] = {
		4
	};
	//Get the size of array
	size = sizeof(arr3) / sizeof(arr3[0]);
	obj.unique_elements(arr3, size);
	return 0;
}

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
//Include namespace system
using System;
/*
  C# Program
  Find unique elements in sorted array
*/
class MyArray
{
	//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");
	}
	//Finding all unique elements in given sorted array
	public void unique_elements(int[] arr, int size)
	{
		if (size < 1)
		{
			return;
		}
		Console.Write("\n Array :");
		display(arr, size);
		Console.Write(" Unique elements : ");
		if (size == 1)
		{
			Console.Write(" " + arr[0] + "\n");
			return;
		}
		int status = 0;
		for (int i = 0; i < size; ++i)
		{
			if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
			{
				Console.Write(" " + arr[i]);
				status = 1;
			}
			else if (i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
			{
				Console.Write(" " + arr[i]);
				status = 1;
			}
		}
		Console.Write("\n");
		if (status == 0)
		{
			Console.Write("\n Not Found");
		}
	}
	public static void Main(String[] args)
	{
		MyArray obj = new MyArray();
		int[] arr1 = {
			1 , 1 , 4 , 6 , 7 , 7 , 9 , 9 , 9
		};
		//Get the size of array
		int size = arr1.Length;
		obj.unique_elements(arr1, size);
		int[] arr2 = {
			-3 , -2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 6
		};
		//Get the size of array
		size = arr2.Length;
		obj.unique_elements(arr2, size);
		int[] arr3 = {
			4
		};
		//Get the size of array
		size = arr3.Length;
		obj.unique_elements(arr3, size);
	}
}

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
<?php
/*
  Php Program
  Find unique elements in sorted array
*/
class MyArray
{
	//Function which is display array elements
	public	function display( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	//Finding all unique elements in given sorted array
	public	function unique_elements( $arr, $size)
	{
		if ($size < 1)
		{
			return;
		}
		echo "\n Array :";
		$this->display($arr, $size);
		echo " Unique elements : ";
		if ($size == 1)
		{
			echo " ". $arr[0] ."\n";
			return;
		}
		$status = 0;
		for ($i = 0; $i < $size; ++$i)
		{
			if (($i == 0 && $i + 1 < $size && $arr[$i] != $arr[$i + 1]) || ($i == $size - 1 && $i > 0 && $arr[$i] != $arr[$i - 1]))
			{
				echo " ". $arr[$i];
				$status = 1;
			}
			else if ($i > 0 && $i + 1 < $size && $arr[$i] != $arr[$i + 1] && $arr[$i] != $arr[$i - 1])
			{
				echo " ". $arr[$i];
				$status = 1;
			}
		}
		echo "\n";
		if ($status == 0)
		{
			echo "\n Not Found";
		}
	}
}

function main()
{
	$obj = new MyArray();
	//Define array of integer elements
	$arr1 = array(1, 1, 4, 6, 7, 7, 9, 9, 9);
	//Get the size of array
	$size = count($arr1);
	$obj->unique_elements($arr1, $size);
	$arr2 = array(-3, -2, 2, 3, 4, 4, 4, 5, 5, 6);
	//Get the size of array
	$size = count($arr2);
	$obj->unique_elements($arr2, $size);
	$arr3 = array(4);
	//Get the size of array
	$size = count($arr3);
	$obj->unique_elements($arr3, $size);
}
main();

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
/*
  Node Js Program
  Find unique elements in sorted array
*/
class MyArray
{
	//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");
	}
	//Finding all unique elements in given sorted array
	unique_elements(arr, size)
	{
		if (size < 1)
		{
			return;
		}
		process.stdout.write("\n Array :");
		this.display(arr, size);
		process.stdout.write(" Unique elements : ");
		if (size == 1)
		{
			process.stdout.write(" " + arr[0] + "\n");
			return;
		}
		var status = 0;
		for (var i = 0; i < size; ++i)
		{
			if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
			{
				process.stdout.write(" " + arr[i]);
				status = 1;
			}
			else if (i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
			{
				process.stdout.write(" " + arr[i]);
				status = 1;
			}
		}
		process.stdout.write("\n");
		if (status == 0)
		{
			process.stdout.write("\n Not Found");
		}
	}
}

function main()
{
	var obj = new MyArray();
	//Define array of integer elements
	var arr1 = [1, 1, 4, 6, 7, 7, 9, 9, 9];
	//Get the size of array
	var size = arr1.length;
	obj.unique_elements(arr1, size);
	var arr2 = [-3, -2, 2, 3, 4, 4, 4, 5, 5, 6];
	//Get the size of array
	size = arr2.length;
	obj.unique_elements(arr2, size);
	var arr3 = [4];
	//Get the size of array
	size = arr3.length;
	obj.unique_elements(arr3, size);
}
main();

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
#   Python 3 Program
#   Find unique elements in sorted array

class MyArray :
	# Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print("\n", end = "")
	
	# Finding all unique elements in given sorted array
	def unique_elements(self, arr, size) :
		if (size < 1) :
			return
		
		print("\n Array :", end = "")
		self.display(arr, size)
		print(" Unique elements : ", end = "")
		if (size == 1) :
			print(" ", arr[0] ,"\n", end = "")
			return
		
		status = 0
		i = 0
		while (i < size) :
			if ((i == 0 and i + 1 < size and arr[i] != arr[i + 1]) or(i == size - 1 and i > 0 and arr[i] != arr[i - 1])) :
				print(" ", arr[i], end = "")
				status = 1
			
			elif(i > 0 and i + 1 < size and arr[i] != arr[i + 1] and arr[i] != arr[i - 1]) :
				print(" ", arr[i], end = "")
				status = 1
			
			i += 1
		
		print("\n", end = "")
		if (status == 0) :
			print("\n Not Found", end = "")
		
	

def main() :
	obj = MyArray()
	# Define array of integer elements
	arr1 = [1, 1, 4, 6, 7, 7, 9, 9, 9]
	# Get the size of array
	size = len(arr1)
	obj.unique_elements(arr1, size)
	arr2 = [-3, -2, 2, 3, 4, 4, 4, 5, 5, 6]
	# Get the size of array
	size = len(arr2)
	obj.unique_elements(arr2, size)
	arr3 = [4]
	# Get the size of array
	size = len(arr3)
	obj.unique_elements(arr3, size)

if __name__ == "__main__": main()

Output

 Array :  1  1  4  6  7  7  9  9  9
 Unique elements :   4  6

 Array :  -3  -2  2  3  4  4  4  5  5  6
 Unique elements :   -3  -2  2  3  6

 Array :  4
 Unique elements :   4
#   Ruby Program
#   Find unique elements in sorted array

class MyArray

	# Function which is display array elements
	def display(arr, size)
	
		i = 0
		while (i < size)
		
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
	# Finding all unique elements in given sorted array
	def unique_elements(arr, size)
	
		if (size < 1)
		
			return
		end
		print("\n Array :")
		self.display(arr, size)
		print(" Unique elements : ")
		if (size == 1)
		
			print(" ", arr[0] ,"\n")
			return
		end
		status = 0
		i = 0
		while (i < size)
		
			if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
			
				print(" ", arr[i])
				status = 1
			elsif(i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
			
				print(" ", arr[i])
				status = 1
			end
			i += 1
		end
		print("\n")
		if (status == 0)
		
			print("\n Not Found")
		end
	end
end
def main()

	obj = MyArray.new()
	# Define array of integer elements
	arr1 = [1, 1, 4, 6, 7, 7, 9, 9, 9]
	# Get the size of array
	size = arr1.length
	obj.unique_elements(arr1, size)
	arr2 = [-3, -2, 2, 3, 4, 4, 4, 5, 5, 6]
	# Get the size of array
	size = arr2.length
	obj.unique_elements(arr2, size)
	arr3 = [4]
	# Get the size of array
	size = arr3.length
	obj.unique_elements(arr3, size)
end
main()

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
/*
  Scala Program
  Find unique elements in sorted array
*/
class MyArray
{
	//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");
	}
	//Finding all unique elements in given sorted array
	def unique_elements(arr: Array[Int], size: Int): Unit = {
		if (size < 1)
		{
			return;
		}
		print("\n Array :");
		display(arr, size);
		print(" Unique elements : ");
		if (size == 1)
		{
			print(" " + arr(0) + "\n");
			return;
		}
		var status: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			if ((i == 0 && i + 1 < size && arr(i) != arr(i + 1)) || (i == size - 1 && i > 0 && arr(i) != arr(i - 1)))
			{
				print(" " + arr(i));
				status = 1;
			}
			else if (i > 0 && i + 1 < size && arr(i) != arr(i + 1) && arr(i) != arr(i - 1))
			{
				print(" " + arr(i));
				status = 1;
			}
			i += 1;
		}
		print("\n");
		if (status == 0)
		{
			print("\n Not Found");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyArray = new MyArray();
		//Define array of integer elements
		var arr1: Array[Int] = Array(1, 1, 4, 6, 7, 7, 9, 9, 9);
		//Get the size of array
		var size: Int = arr1.length;
		obj.unique_elements(arr1, size);
		var arr2: Array[Int] = Array(-3, -2, 2, 3, 4, 4, 4, 5, 5, 6);
		//Get the size of array
		size = arr2.length;
		obj.unique_elements(arr2, size);
		var arr3: Array[Int] = Array(4);
		//Get the size of array
		size = arr3.length;
		obj.unique_elements(arr3, size);
	}
}

Output

 Array : 1 1 4 6 7 7 9 9 9
 Unique elements :  4 6

 Array : -3 -2 2 3 4 4 4 5 5 6
 Unique elements :  -3 -2 2 3 6

 Array : 4
 Unique elements :  4
/*
  Swift Program
  Find unique elements in sorted array
*/
class MyArray
{
	//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("\n", terminator: "");
	}
	//Finding all unique elements in given sorted array
	func unique_elements(_ arr: [Int], _ size: Int)
	{
		if (size < 1)
		{
			return;
		}
		print("\n Array :", terminator: "");
		self.display(arr, size);
		print(" Unique elements : ", terminator: "");
		if (size == 1)
		{
			print(" ", arr[0] ,"\n", terminator: "");
			return;
		}
		var status: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			if ((i == 0 && i + 1 < size && arr[i] != arr[i + 1]) || (i == size - 1 && i > 0 && arr[i] != arr[i - 1]))
			{
				print(" ", arr[i], terminator: "");
				status = 1;
			}
			else if (i > 0 && i + 1 < size && arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
			{
				print(" ", arr[i], terminator: "");
				status = 1;
			}
			i += 1;
		}
		print("\n", terminator: "");
		if (status == 0)
		{
			print("\n Not Found", terminator: "");
		}
	}
}
func main()
{
	let obj: MyArray = MyArray();
	//Define array of integer elements
	let arr1: [Int] = [1, 1, 4, 6, 7, 7, 9, 9, 9];
	//Get the size of array
	var size: Int = arr1.count;
	obj.unique_elements(arr1, size);
	let arr2: [Int] = [-3, -2, 2, 3, 4, 4, 4, 5, 5, 6];
	//Get the size of array
	size = arr2.count;
	obj.unique_elements(arr2, size);
	let arr3: [Int] = [4];
	//Get the size of array
	size = arr3.count;
	obj.unique_elements(arr3, size);
}
main();

Output

 Array :  1  1  4  6  7  7  9  9  9
 Unique elements :   4  6

 Array :  -3  -2  2  3  4  4  4  5  5  6
 Unique elements :   -3  -2  2  3  6

 Array :  4
 Unique elements :   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