Posted on by Kalkicode
Code Array

Remove duplicates from sorted array

Here given code implementation process.

/*
    Java program
    Remove duplicates from sorted array
*/
public class Duplicates
{
	// Display array elements
	public void printData(int[] arr, int n)
	{
		System.out.print("\n Array : ");
		for (int i = 0; i < n; ++i)
		{
			// print element
			System.out.print(" " + arr[i]);
		}
	}
	public int removeDuplicates(int[] arr, int n)
	{
		int position = 1;
		int current = arr[0];
      	// Execute the loop through from (1..n)
		for (int i = 1; i < n; ++i)
		{
			if (current != arr[i])
			{
				arr[position] = arr[i];
              	// Get new element
				current = arr[i];
				position++;
			}
		}
		return position;
	}
	public static void main(String[] args)
	{
      	Duplicates task = new Duplicates();
		int[] arr = {
			1 , 2 , 2 , 4 , 4 , 5 , 6 , 6 , 6 , 9
		};
		// Get the size of array
		int n = arr.length;
		// Display array element
		task.printData(arr, n);
		// Remove duplicate
		n = task.removeDuplicates(arr, n);
		// Display array element
		task.printData(arr, n);
	}
}

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program
    Remove duplicates from sorted array
*/
class Duplicates
{
	public:
		// Display array elements
		void printData(int arr[], int n)
		{
			cout << "\n Array : ";
			for (int i = 0; i < n; ++i)
			{
				// print element
				cout << " " << arr[i];
			}
		}
	int removeDuplicates(int arr[], int n)
	{
		int position = 1;
		int current = arr[0];
		// Execute the loop through from (1..n)
		for (int i = 1; i < n; ++i)
		{
			if (current != arr[i])
			{
				arr[position] = arr[i];
				// Get new element
				current = arr[i];
				position++;
			}
		}
		return position;
	}
};
int main()
{
	Duplicates *task = new Duplicates();
	int arr[] = {
		1 , 2 , 2 , 4 , 4 , 5 , 6 , 6 , 6 , 9
	};
	// Get the size of array
	int n = sizeof(arr) / sizeof(arr[0]);
	// Display array element
	task->printData(arr, n);
	// Remove duplicate
	n = task->removeDuplicates(arr, n);
	// Display array element
	task->printData(arr, n);
	return 0;
}

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
// Include namespace system
using System;
/*
    Csharp program
    Remove duplicates from sorted array
*/
public class Duplicates
{
	// Display array elements
	public void printData(int[] arr, int n)
	{
		Console.Write("\n Array : ");
		for (int i = 0; i < n; ++i)
		{
			// print element
			Console.Write(" " + arr[i]);
		}
	}
	public int removeDuplicates(int[] arr, int n)
	{
		int position = 1;
		int current = arr[0];
		// Execute the loop through from (1..n)
		for (int i = 1; i < n; ++i)
		{
			if (current != arr[i])
			{
				arr[position] = arr[i];
				// Get new element
				current = arr[i];
				position++;
			}
		}
		return position;
	}
	public static void Main(String[] args)
	{
		Duplicates task = new Duplicates();
		int[] arr = {
			1 , 2 , 2 , 4 , 4 , 5 , 6 , 6 , 6 , 9
		};
		// Get the size of array
		int n = arr.Length;
		// Display array element
		task.printData(arr, n);
		// Remove duplicate
		n = task.removeDuplicates(arr, n);
		// Display array element
		task.printData(arr, n);
	}
}

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
package main
import "fmt"
/*
    Go program
    Remove duplicates from sorted array
*/
// Display array elements
func printData(arr[] int) {
	var n int = len(arr)
	fmt.Print("\n Array : ")
	for i := 0 ; i < n ; i++ {
		// print element
		fmt.Print(" ", arr[i])
	}
}
func removeDuplicates(arr[] int) [] int {
	var n int = len(arr)
	var position int = 1
	var current int = arr[0]
	// Execute the loop through from (1..n)
	for i := 1 ; i < n ; i++ {
		if current != arr[i] {
			arr[position] = arr[i]
			// Get new element
			current = arr[i]
			position++
		}
	}
	if position < n {
		arr = append(arr[:position], arr[n:]...)
	}
	return arr
}
func main() {
	
	var arr = [] int {
		1,
		2,
		2,
		4,
		4,
		5,
		6,
		6,
		6,
		9,
	}
	// Display array element
	printData(arr)
	// Remove duplicate
	arr = removeDuplicates(arr)
	// Display array element
	printData(arr)
}

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
<?php
/*
    Php program
    Remove duplicates from sorted array
*/
class Duplicates
{
	// Display array elements
	public	function printData($arr)
	{
		$n = count($arr);
		echo("\n Array : ");
		for ($i = 0; $i < $n; ++$i)
		{
			// print element
			echo(" ".$arr[$i]);
		}
	}
	public	function removeDuplicates(&$arr)
	{
		$n = count($arr);
		$position = 1;
		$current = $arr[0];
		// Execute the loop through from (1..n)
		for ($i = 1; $i < $n; ++$i)
		{
			if ($current != $arr[$i])
			{
				$arr[$position] = $arr[$i];
				// Get new element
				$current = $arr[$i];
				$position++;
			}
		}
      	if($position!=$n)
        {
           // Remove elements 
          	array_splice($arr, $position);
        }
	}
}

function main()
{
	$task = new Duplicates();
	$arr = array(1, 2, 2, 4, 4, 5, 6, 6, 6, 9);
	// Display array element
	$task->printData($arr);
	// Remove duplicate
	$task->removeDuplicates($arr);
	// Display array element
	$task->printData($arr);
}
main();

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
/*
    Node JS program
    Remove duplicates from sorted array
*/
class Duplicates
{
	// Display array elements
	printData(arr)
	{
		var n = arr.length;
		process.stdout.write("\n Array : ");
		for (var i = 0; i < n; ++i)
		{
			// print element
			process.stdout.write(" " + arr[i]);
		}
	}
	removeDuplicates(arr)
	{
		var n = arr.length;
		var position = 1;
		var current = arr[0];
		// Execute the loop through from (1..n)
		for (var i = 1; i < n; ++i)
		{
			if (current != arr[i])
			{
				arr[position] = arr[i];
				// Get new element
				current = arr[i];
				position++;
			}
		}
      	if(position < n)
        {
            arr.splice(position, n-position);
        }
	}
}

function main()
{
	var task = new Duplicates();
	var arr = [1, 2, 2, 4, 4, 5, 6, 6, 6, 9];
	// Display array element
	task.printData(arr);
	// Remove duplicate
	task.removeDuplicates(arr);
	// Display array element
	task.printData(arr);
}
main();

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
#    Python 3 program
#    Remove duplicates from sorted array
class Duplicates :
    #  Display list elements
    def printData(self, arr) :
        n = len(arr)
        print("\n Array : ", end = "")
        i = 0
        while (i < n) :
            #  print element
            print(" ", arr[i], end = "")
            i += 1
        
    
    def removeDuplicates(self, arr) :
        n = len(arr)
        position = 1
        current = arr[0]
        i = 1
        #  Execute the loop through from (1..n)
        while (i < n) :
            if (current != arr[i]) :
                arr[position] = arr[i]
                #  Get new element
                current = arr[i]
                position += 1
            
            i += 1
        if (position < n) :
            del arr[position:]
    

def main() :
    task = Duplicates()
    arr = [1, 2, 2, 4, 4, 5, 6, 6, 6, 9]
    #  Display list element
    task.printData(arr)
    #  Remove duplicate
    task.removeDuplicates(arr)
    #  Display list element
    task.printData(arr)

if __name__ == "__main__": main()

input

 Array :   1  2  2  4  4  5  6  6  6  9
 Array :   1  2  4  5  6  9
#    Ruby program
#    Remove duplicates from sorted array
class Duplicates 
	#  Display array elements
	def printData(arr) 
		n = arr.length
		print("\n Array : ")
		i = 0
		while (i < n) 
			#  print element
			print(" ", arr[i])
			i += 1
		end

	end

	def removeDuplicates(arr) 
		n = arr.length
		position = 1
		current = arr[0]
		i = 1
		#  Execute the loop through from (1..n)
		while (i < n) 
			if (current != arr[i]) 
				arr[position] = arr[i]
				#  Get new element
				current = arr[i]
				position += 1
			end

			i += 1
		end
        if ( position < n )
        	arr = arr[0, position]
        end
   
		return arr
	end

end

def main() 
	task = Duplicates.new()
	arr = [1, 2, 2, 4, 4, 5, 6, 6, 6, 9]
	#  Display array element
	task.printData(arr)
	#  Remove duplicate
	arr = task.removeDuplicates(arr)
	#  Display array element
	task.printData(arr)
end

main()

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9
import Foundation;
/*
    Swift 4 program
    Remove duplicates from sorted array
*/
class Duplicates
{
	// Display array elements
	func printData(_ arr: [Int])
	{
		let n: Int = arr.count;
		print("\n Array : ", terminator: "");
		var i: Int = 0;
		while (i < n)
		{
			// print element
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func removeDuplicates(_ arr: inout[Int])
	{
		let n: Int = arr.count;
		var position: Int = 1;
		var current: Int = arr[0];
		var i: Int = 1;
		// Execute the loop through from (1..n)
		while (i < n)
		{
			if (current  != arr[i])
			{
				arr[position] = arr[i];
				// Get new element
				current = arr[i];
				position += 1;
			}
			i += 1;
		}
		if (position < n)
		{	
          	// Remove last remaining nodes
			while (position < n)
			{
				arr.removeLast();
				position += 1;
			}
		}
	}
}
func main()
{
	let task: Duplicates = Duplicates();
	var arr: [Int] = [1, 2, 2, 4, 4, 5, 6, 6, 6, 9];
	// Display array element
	task.printData(arr);
	// Remove duplicate
	task.removeDuplicates( &arr);
	// Display array element
	task.printData(arr);
}
main();

input

 Array :   1  2  2  4  4  5  6  6  6  9
 Array :   1  2  4  5  6  9
/*
    Scala program
    Remove duplicates from sorted array
*/
class Duplicates()
{
	// Display array elements
	def printData(arr: Array[Int]): Unit = {
		var n: Int = arr.length;
		print("\n Array : ");
		var i: Int = 0;
		while (i < n)
		{
			// print element
			print(" " + arr(i));
			i += 1;
		}
	}
	def removeDuplicates(arr: Array[Int]): Array[Int] = {
		var n: Int = arr.length;
		var position: Int = 1;
		var current: Int = arr(0);
		var i: Int = 1;
		// Execute the loop through from (1..n)
		while (i < n)
		{
			if (current != arr(i))
			{
				arr(position) = arr(i);
				// Get new element
				current = arr(i);
				position += 1;
			}
			i += 1;
		}
		return  arr.take(position);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Duplicates = new Duplicates();
		var arr: Array[Int] = Array(1, 2, 2, 4, 4, 5, 6, 6, 6, 9);
		// Display array element
		task.printData(arr);
		// Remove duplicate
		arr = task.removeDuplicates(arr);
		// Display array element
		task.printData(arr);
	}
}

input

 Array :  1 2 2 4 4 5 6 6 6 9
 Array :  1 2 4 5 6 9

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