Posted on by Kalkicode
Code Array

Find element that appears only once in array

Finding an element that appears only once in an array is a common programming problem. The goal is to identify the element in the array that doesn't have any duplicate occurrences.

Problem Statement

Given an array of integers, find and display all the elements that appear only once in the array.

Example

Consider the following array:

arr = [8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7]

The elements that appear only once in this array are: 3, 2, 4, -5, 1, 9, and 7.

Idea to Solve

Iterate through the array and for each element, check if it has any duplicates by comparing it with all other elements in the array.

Pseudocode

function unique_element(arr, size):
    for i from 0 to size - 1:
        status = 1
        for j from 0 to size - 1:
            if i ≠ j and arr[i] = arr[j]:
                status = 0
                break
        if status = 1:
            print(arr[i])

// Example usage
arr = [8, 3, 2, 6, 4, -5, 1, 6, 8, 9, 7]
size = size(arr)
unique_element(arr, size)

Algorithm Explanation

  1. The unique_element function takes the array and its size as parameters.
  2. For each element in the array, it initializes status to 1, indicating that the element is unique until proven otherwise.
  3. It compares the current element with all other elements in the array using a nested loop.
  4. If it finds a duplicate, it sets status to 0 and breaks out of the inner loop.
  5. After checking all other elements, if status is still 1, it means the current element is unique and it prints the element.

Code Solution

//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

Time Complexity

The algorithm compares each element with all other elements, resulting in a nested loop structure. Therefore, the time complexity of the algorithm is O(n^2), where n is the size of the array.

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.

New Comment