Posted on by Kalkicode
Code Hash

# Find the unique elements in array

Here given code implementation process.

``````/*
Java program
Find the unique elements in array
*/
import java.util.HashSet;
public class Finding
{
//This function are displaying given 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");
}
//  Function which is find all unique elements in array
public void findUnique(int[] arr)
{
int n = arr.length;
if (n == 0)
{
System.out.print(" Empty Array");
return;
}
// Use to get unique element
HashSet < Integer > result = new HashSet < Integer > ();
for (int i = 0; i < n; ++i)
{
// Get unique values in array
}
// Display array elements
System.out.print("\n Array : ");
this.display(arr, n);

System.out.print(" Unique elements : ");
// Display unique elements
for (int value: result)
{
System.out.print("  " + value);
}
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
1 , 5 , 7 , 2 , 1 , 3 , 1 , 5
};
int[] arr2 = {
1 , 2 , 2 , 2 , 2 , 1
};
//Test case
}
}``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :   1  2  3  5  7
Array :  1 2 2 2 2 1
Unique elements :   1  2``````
``````// Include header file
#include <iostream>
#include <set>
#include <iterator>
using namespace std;

class Finding
{
public:
//This function are displaying given array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
//  Function which is find all unique elements in array
void findUnique(int arr[], int n)
{
if (n == 0)
{
cout << " Empty Array";
return;
}
// Use to get unique element
set <int> result ;
set <int> :: iterator itr;
for (int i = 0; i < n; ++i)
{
result.insert(arr[i]);
}
// Display array elements
cout << "\n Array : ";
this->display(arr, n);

cout << " Unique elements : ";
// Display unique elements
for (itr = result.begin(); itr != result.end(); ++itr)
{
cout << "  " << *itr;
}
}
};
int main()
{
// Define array of integer elements
int arr1[] = {
1 , 5 , 7 , 2 , 1 , 3 , 1 , 5
};
int arr2[] = {
1 , 2 , 2 , 2 , 2 , 1
};
//Test case
int n = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
return 0;
}``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :   1  2  3  5  7
Array :  1 2 2 2 2 1
Unique elements :   1  2``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# program
Find the unique elements in array
*/
public class Finding
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
//  Function which is find all unique elements in array
public void findUnique(int[] arr)
{
int n = arr.Length;
if (n == 0)
{
Console.Write(" Empty Array");
return;
}
// Use to get unique element
HashSet <int> result = new HashSet <int> ();
for (int i = 0; i < n; ++i)
{
}
// Display array elements
Console.Write("\n Array : ");
this.display(arr, n);
Console.Write(" Unique elements : ");
// Display unique elements
foreach (int value in result)
{
Console.Write("  " + value);
}
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
1 , 5 , 7 , 2 , 1 , 3 , 1 , 5
};
int[] arr2 = {
1 , 2 , 2 , 2 , 2 , 1
};
//Test case
}
}``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :   1  5  7  2  3
Array :  1 2 2 2 2 1
Unique elements :   1  2``````
``````<?php
/*
Php program
Find the unique elements in array
*/
class Finding
{
//This function are displaying given array elements
public	function display( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ". \$arr[\$i];
}
echo "\n";
}
//  Function which is find all unique elements in array
public	function findUnique( & \$arr)
{
\$n = count(\$arr);
if (\$n == 0)
{
echo " Empty Array";
return;
}
// Use to get unique element
\$result = array();
for (\$i = 0; \$i < \$n; ++\$i)
{
if (in_array(\$arr[\$i], \$result,True) == false)
{
\$result[] = \$arr[\$i];
}
}
// Display array elements
echo "\n Array : ";
\$this->display(\$arr, \$n);
echo " Unique elements : ";
// Display unique elements
foreach(\$result as \$key => \$value)
{
echo "  ". \$value;
}
}
}

function main()
{
// Define array of integer elements
\$arr1 = array(1, 5, 7, 2, 1, 3, 1, 5);
\$arr2 = array(1, 2, 2, 2, 2, 1);
//Test case
}
main();``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :   1  5  7  2  3
Array :  1 2 2 2 2 1
Unique elements :   1  2``````
``````/*
Node Js program
Find the unique elements in array
*/
class Finding
{
//This function are displaying given array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
//  Function which is find all unique elements in array
findUnique(arr)
{
var n = arr.length;
if (n == 0)
{
process.stdout.write(" Empty Array");
return;
}
// Use to get unique element
var result = new Set();
for (var i = 0; i < n; ++i)
{
}
// Display array elements
process.stdout.write("\n Array : ");
this.display(arr, n);
process.stdout.write(" Unique elements : ");
// Display unique elements
for (let value of result.values())
{
process.stdout.write(" " + value);
}
}
}

function main()
{
// Define array of integer elements
var arr1 = [1, 5, 7, 2, 1, 3, 1, 5];
var arr2 = [1, 2, 2, 2, 2, 1];
//Test case
}
main();``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :  1 5 7 2 3
Array :  1 2 2 2 2 1
Unique elements :  1 2``````
``````#   Python 3 program
#   Find the unique elements in array

class Finding :
# This function are displaying given list elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

#   Function which is find all unique elements in list
def findUnique(self, arr) :
n = len(arr)
if (n == 0) :
print(" Empty Array", end = "")
return

#  Use to get unique element
result = set()
i = 0
while (i < n) :
#  Get unique values of list
i += 1

#  Display list elements
print("\n Array : ", end = "")
self.display(arr, n)
print(" Unique elements : ", end = "")
#  Display unique elements
print(result)

def main() :
#  Define list of integer elements
arr1 = [1, 5, 7, 2, 1, 3, 1, 5]
arr2 = [1, 2, 2, 2, 2, 1]
# Test case

if __name__ == "__main__": main()``````

#### Output

`````` Array :   1  5  7  2  1  3  1  5
Unique elements : {1, 2, 3, 5, 7}

Array :   1  2  2  2  2  1
Unique elements : {1, 2}``````
``````#   Ruby program
#   Find the unique elements in array

class Finding
# This function are displaying given array elements
def display(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end

print("\n")
end

#   Function which is find all unique elements in array
def findUnique(arr)
n = arr.length
if (n == 0)
print(" Empty Array")
return
end

#  Use to get unique element
result = []
i = 0
while (i < n)
if (result.include?(arr[i])==false)
result.push(arr[i])
end
i += 1
end

#  Display array elements
print("\n Array : ")
self.display(arr, n)
print(" Unique elements : ")
#  Display unique elements
print(result)

end

end

def main()
#  Define array of integer elements
arr1 = [1, 5, 7, 2, 1, 3, 1, 5]
arr2 = [1, 2, 2, 2, 2, 1]
# Test case
end

main()``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements : [1, 5, 7, 2, 3]
Array :  1 2 2 2 2 1
Unique elements : [1, 2]``````
``````import scala.collection.mutable._;
/*
Scala program
Find the unique elements in array
*/
class Finding
{
//This function are displaying given array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
//  Function which is find all unique elements in array
def findUnique(arr: Array[Int]): Unit = {
var n: Int = arr.length;
if (n == 0)
{
print(" Empty Array");
return;
}
// Use to get unique element
var result: Set[Int] = Set();
var i: Int = 0;
while (i < n)
{
i += 1;
}
// Display array elements
print("\n Array : ");
this.display(arr, n);
print(" Unique elements : ");
// Display unique elements
for (value <- result) print(" "+value)
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Finding = new Finding();
// Define array of integer elements
var arr1: Array[Int] = Array(1, 5, 7, 2, 1, 3, 1, 5);
var arr2: Array[Int] = Array(1, 2, 2, 2, 2, 1);
//Test case
}
}``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :  1 2 3 5 7
Array :  1 2 2 2 2 1
Unique elements :  1 2``````
``````/*
Swift 4 program
Find the unique elements in array
*/
class Finding
{
//This function are displaying given array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
//  Function which is find all unique elements in array
func findUnique(_ arr: [Int])
{
let n: Int = arr.count;
if (n == 0)
{
print(" Empty Array", terminator: "");
return;
}
// Use to get unique element
var result = Set<Int>()
var i: Int = 0;
while (i < n)
{
result.insert(arr[i]);
i += 1;
}
// Display array elements
print("\n Array : ", terminator: "");
self.display(arr, n);
print(" Unique elements : ", terminator: "");
// Display unique elements
for value in result
{
print("  ", value, terminator: "");
}
}
}
func main()
{
// Define array of integer elements
let arr1: [Int] = [1, 5, 7, 2, 1, 3, 1, 5];
let arr2: [Int] = [1, 2, 2, 2, 2, 1];
//Test case
}
main();``````

#### Output

`````` Array :   1  5  7  2  1  3  1  5
Unique elements :    5   7   2   3   1
Array :   1  2  2  2  2  1
Unique elements :    2   1``````
``````/*
Kotlin program
Find the unique elements in array
*/
class Finding
{
//This function are displaying given array elements
fun display(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
//  Function which is find all unique elements in array
fun findUnique(arr: Array <Int> ): Unit
{
var n: Int = arr.count();
if (n == 0)
{
print(" Empty Array");
return;
}
// Use to get unique element
var result: MutableSet <Int> = mutableSetOf <Int> ();
var i: Int = 0;
while (i < n)
{
i += 1;
}
// Display array elements
print("\n Array : ");
this.display(arr, n);
print(" Unique elements : ");
// Display unique elements
for (value in result)
{
print("  " + value);
}
}
}
fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var arr1: Array < Int > = arrayOf(1, 5, 7, 2, 1, 3, 1, 5);
var arr2: Array < Int > = arrayOf(1, 2, 2, 2, 2, 1);
//Test case
}``````

#### Output

`````` Array :  1 5 7 2 1 3 1 5
Unique elements :   1  5  7  2  3
Array :  1 2 2 2 2 1
Unique elements :   1  2``````

## Comment

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.

Categories
Relative Post