Find element that appears only once in array

Here given code implementation process.

//C Program 
//Find element that appears only once in array
#include <stdio.h>

//Find and display all non repeated element
void unique_element(int arr[],int size)
{
  
  if(size<=0)
  {
    //When invalid size of array
    return;
  }
  else
  {
    int status = 1;

    for (int i = 0; i < size; ++i)
    {
      //initial set element is a unique
      status = 1;

      for (int j = 0; j < size && status == 1; ++j)
      {
        if(i!=j && arr[i]==arr[j])
        {
          //When find duplicate elements
          status=0;
        }
      }
      if(status==1)
      {
        //When get a unique
        printf("  %d",arr[i] );
      }
    }
    
  }
}


int main()
{
  //Define array elements
  int arr[] ={8,3,2,6,4,-5,1,6,8,9,7};
  
  //Get the size of array elements
  int size=sizeof(arr) / sizeof(arr[0]);

  unique_element(arr,size);

  return 0;
  
}

Output

  3  2  4  -5  1  9  7
/*
  C++ Program
  Find element that appears only once in array
*/
#include<iostream>
using namespace std;

class MyArray {
	public:

    //Find and display all non repeated element
    void unique_element(int arr[], int size) {
      if (size <= 0) {
        return;
      } else {
        int status = 1;
        for (int i = 0; i < size; ++i) {
          //initial set element is a unique
          status = 1;
          for (int j = 0; j < size && status == 1; ++j) {
            if (i != j && arr[i] == arr[j]) {
              //When find duplicate elements
              status = 0;
            }
          }
          if (status == 1) {
            //When get a unique

            cout << " " << arr[i];
          }
        }
      }
    }
};
int main() {
	MyArray obj ;
	int arr[] = {
		8,
		3,
		2,
		6,
		4,
		-5,
		1,
		6,
		8,
		9,
		7
	};
	//Count size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	obj.unique_element(arr, size);
	return 0;
}

Output

 3 2 4 -5 1 9 7
/*
  Java Program
  Find element that appears only once in array
*/
public class MyArray {

  //Find and display all non repeated element
  public void unique_element(int []arr,int size)
  {
    
    if(size<=0)
    {
      //When invalid size of array
      return;
    }
    else
    {
      int status = 1;

      for (int i = 0; i < size; ++i)
      {
        //initial set element is a unique
        status = 1;

        for (int j = 0; j < size && status == 1; ++j)
        {
          if(i!=j && arr[i]==arr[j])
          {
            //When find duplicate elements
            status=0;
          }
        }
        if(status==1)
        {
          //When get a unique
          System.out.print("  "+arr[i] );
        }
      }
      
    }
  }

  public static void main(String[] args) 
  {

    MyArray obj = new MyArray();
    //Define array elements
    int []arr =  {8,3,2,6,4,-5,1,6,8,9,7};
    //Count size of array
    int size=arr.length;
    obj.unique_element(arr,size);

  }
}

Output

 3 2 4 -5 1 9 7
using System;

/*
  C# Program
  Find element that appears only once in array
*/

public class MyArray {
	//Find and display all non repeated element
	public void unique_element(int[] arr, int size) {
		if (size <= 0) {
			return;
		} else {
			int status = 1;
			for (int i = 0; i < size; ++i) {
				//initial set element is a unique
				status = 1;
				for (int j = 0; j < size && status == 1; ++j) {
					if (i != j && arr[i] == arr[j]) {
						//When find duplicate elements
						status = 0;
					}
				}
				if (status == 1) {
					Console.Write(" " + arr[i]);
				}
			}
		}
	}
	public static void Main(String[] args) {
		MyArray obj = new MyArray();
      	//Define array elements
		int[] arr = {
			8,
			3,
			2,
			6,
			4,
			-5,
			1,
			6,
			8,
			9,
			7
		};
		//Count size of array
		int size = arr.Length;
		obj.unique_element(arr, size);
	}
}

Output

 3 2 4 -5 1 9 7
<?php
/*
  Php Program
  Find element that appears only once in array
*/
class MyArray {
	//Find and display all non repeated element

	public 	function unique_element($arr, $size) {
		if ($size <= 0) {
			return;
		} else {
			$status = 1;
			for ($i = 0; $i < $size; ++$i) {
				//initial set element is a unique
				$status = 1;
				for ($j = 0; $j < $size && $status == 1; ++$j) {
					if ($i != $j && $arr[$i] == $arr[$j]) {
						//When find duplicate elements
						$status = 0;
					}
				}
				if ($status == 1) {
					//When get a unique

					echo(" ". $arr[$i]);
				}
			}
		}
	}
}

function main() {
	$obj = new MyArray();
	//Define array elements
	$arr = array(8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7);
	//Count size of array
	$size = count($arr);
	$obj->unique_element($arr, $size);

}
main();

Output

 3 2 4 -5 1 9 7
/*
  Node Js Program
  Find element that appears only once in array
*/
class MyArray {
	//Find and display all non repeated element
	unique_element(arr, size) {
		if (size <= 0) {
			return;
		} else {
			var status = 1;
			for (var i = 0; i < size; ++i) {
				//initial set element is a unique
				status = 1;
				for (var j = 0; j < size && status == 1; ++j) {
					if (i != j && arr[i] == arr[j]) {
						//When find duplicate elements
						status = 0;
					}
				}

				if (status == 1) {
					//When get a unique

					process.stdout.write(" " + arr[i]);
				}
			}
		}
	}
}

function main(args) {
	var obj = new MyArray();
	//Define array elements
	var arr = [8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7];
	//Count size of array
	var size = arr.length;
	obj.unique_element(arr, size);
}

main();

Output

 3 2 4 -5 1 9 7
# Python 3 Program
# Find element that appears only once in array
class MyArray :
	# Find and display all non repeated element
	def unique_element(self, arr, size) :
		if (size <= 0) :
			return
		else :
			status = 1
			i = 0
			while (i < size) :
				# initial set element is a unique
				status = 1
				j = 0
				while (j < size and status == 1) :
					if (i != j and arr[i] == arr[j]) :
						# When find duplicate elements
						status = 0
					
					j += 1
				
				if (status == 1) :
					print(" ", arr[i], end = "")
				
				i += 1
			
		
	

def main() :
	obj = MyArray()
	arr = [8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7]
	size = len(arr)
	obj.unique_element(arr, size)


if __name__ == "__main__":
	main()

Output

  3  2  4  -5  1  9  7
# Ruby Program
# Find element that appears only once in array
class MyArray 
	# Find and display all non repeated element
	def unique_element(arr, size) 
		if (size <= 0) 
			return
		else 
			status = 1
			i = 0
			while (i < size) 
				# initial set element is a unique
				status = 1
				j = 0
				while (j < size && status == 1) 
					if (i != j && arr[i] == arr[j]) 
						# When find duplicate elements
						status = 0
					end
					j += 1
				end
				if (status == 1) 
					print(" ", arr[i])
				end
				i += 1
			end
		end
	end
end
def main() 
	obj = MyArray.new()
	arr = [8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7]
	size = arr.length
	obj.unique_element(arr, size)
end
main()

Output

 3 2 4 -5 1 9 7
/*
  Scala Program
  Find element that appears only once in array
*/
class MyArray {
	//Find and display all non repeated element
	def unique_element(arr: Array[Int], size: Int): Unit = {
		if (size <= 0) {
			return;
		} else {
			var status: Int = 1;
			var i: Int = 0;
			while (i < size) {
				//initial set element is a unique
				status = 1;
				var j: Int = 0;
				while (j < size && status == 1) {
					if (i != j && arr(i) == arr(j)) {
						//When find duplicate elements
						status = 0;
					}
					j += 1;
				}
				if (status == 1) {
					print(" " + arr(i));
				}
				i += 1;
			}
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyArray = new MyArray();
		val arr: Array[Int] = Array(8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7);
		val size: Int = arr.length;
		obj.unique_element(arr, size);
	}
}

Output

 3 2 4 -5 1 9 7
/*
  Swift Program
  Find element that appears only once in array
*/
class MyArray {
	//Find and display all non repeated element
	func unique_element(_ arr: [Int], _ size: Int) {
		if (size <= 0) {
			return;
		} else {
			var status: Int = 1;
			var i: Int = 0;
			while (i < size) {
				//initial set element is a unique
				status = 1;
				var j: Int = 0;
				while (j < size && status == 1) {
					if (i != j && arr[i] == arr[j]) {
						//When find duplicate elements
						status = 0;
					}
					j += 1;
				}
				if (status == 1) {
					print(" ", arr[i], terminator: "");
				}
				i += 1;
			}
		}
	}
}
func main() {
	let obj: MyArray = MyArray();
	let arr: [Int] = [8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7];
	let size: Int = arr.count;
	obj.unique_element(arr, size);
}
main();

Output

  3  2  4  -5  1  9  7


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