Skip to main content

Move a given element from end of arrays

Here given code implementation process.

// C Program 
// Move a given element from end of arrays
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// Swap the elements of array by given location
void swapElement(int arr[], int front, int tail)
{
	int temp = arr[front];
	arr[front] = arr[tail];
	arr[tail] = temp;
}
// Move given element k to end of array
void moveElement(int arr[], int size, int k)
{
	int head = 0;
	int tail = size - 1;
	while (head < tail)
	{
		if (arr[head] != k)
		{
			head++;
		}
		else if (arr[tail] == k)
		{
			tail--;
		}
		else
		{
			// Swap the elements
			swapElement(arr, head, tail);
			head++;
			tail--;
		}
	}
}
int main(int argc, char
	const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		6 , 0 , 6 , 6 , 3 , 1 , 6 , 5 , 6 , 7 , 1
	};
	// Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	int k = 6;
	// Display array element
	printf("\n  Array elements \n");
	printArray(arr, size);
	moveElement(arr, size, k);
	// Display array elements
	printf("\n  After Move element %d to end\n", k);
	printArray(arr, size);
	return 0;
}

Output

  Array elements
  6  0  6  6  3  1  6  5  6  7  1

  After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
/*
    Java Program
    Move a given element from end of arrays
*/
public class ShiftElements
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Swap the elements of array by given location
	public void swapElement(int[] arr, int front, int tail)
	{
		int temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move given element k to end of array
	public void moveElement(int[] arr, int size, int k)
	{
		int head = 0;
		int tail = size - 1;
		while (head < tail)
		{
			if (arr[head] != k)
			{
				head++;
			}
			else if (arr[tail] == k)
			{
				tail--;
			}
			else
			{
				// Swap the elements
				swapElement(arr, head, tail);
				head++;
				tail--;
			}
		}
	}
	public static void main(String[] args)
	{
		ShiftElements task = new ShiftElements();
		// Define array of integer elements
		int[] arr = {
			6 , 0 , 6 , 6 , 3 , 1 , 6 , 5 , 6 , 7 , 1
		};
		// Get the size of array
		int size = arr.length;
		int k = 6;
		// Display array element
		System.out.print("\n Array elements \n");
		task.printArray(arr, size);
		task.moveElement(arr, size, k);
		// Display array elements
		System.out.print("\n After Move element " + k + " to end\n");
		task.printArray(arr, size);
	}
}

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Move a given element from end of arrays
*/
class ShiftElements
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Swap the elements of array by given location
	void swapElement(int arr[], int front, int tail)
	{
		int temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move given element k to end of array
	void moveElement(int arr[], int size, int k)
	{
		int head = 0;
		int tail = size - 1;
		while (head < tail)
		{
			if (arr[head] != k)
			{
				head++;
			}
			else if (arr[tail] == k)
			{
				tail--;
			}
			else
			{
				// Swap the elements
				this->swapElement(arr, head, tail);
				head++;
				tail--;
			}
		}
	}
};
int main()
{
	ShiftElements task = ShiftElements();
	// Define array of integer elements
	int arr[] = {
		6 , 0 , 6 , 6 , 3 , 1 , 6 , 5 , 6 , 7 , 1
	};
	// Get the size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	int k = 6;
	// Display array element
	cout << "\n Array elements \n";
	task.printArray(arr, size);
	task.moveElement(arr, size, k);
	// Display array elements
	cout << "\n After Move element " << k << " to end\n";
	task.printArray(arr, size);
	return 0;
}

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
// Include namespace system
using System;
/*
    C# Program
    Move a given element from end of arrays
*/
public class ShiftElements
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Swap the elements of array by given location
	public void swapElement(int[] arr, int front, int tail)
	{
		int temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move given element k to end of array
	public void moveElement(int[] arr, int size, int k)
	{
		int head = 0;
		int tail = size - 1;
		while (head < tail)
		{
			if (arr[head] != k)
			{
				head++;
			}
			else if (arr[tail] == k)
			{
				tail--;
			}
			else
			{
				// Swap the elements
				swapElement(arr, head, tail);
				head++;
				tail--;
			}
		}
	}
	public static void Main(String[] args)
	{
		ShiftElements task = new ShiftElements();
		// Define array of integer elements
		int[] arr = {
			6 , 0 , 6 , 6 , 3 , 1 , 6 , 5 , 6 , 7 , 1
		};
		// Get the size of array
		int size = arr.Length;
		int k = 6;
		// Display array element
		Console.Write("\n Array elements \n");
		task.printArray(arr, size);
		task.moveElement(arr, size, k);
		// Display array elements
		Console.Write("\n After Move element " + k + " to end\n");
		task.printArray(arr, size);
	}
}

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
<?php
/*
    Php Program
    Move a given element from end of arrays
*/
class ShiftElements
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Swap the elements of array by given location
	public	function swapElement( & $arr, $front, $tail)
	{
		$temp = $arr[$front];
		$arr[$front] = $arr[$tail];
		$arr[$tail] = $temp;
	}
	// Move given element k to end of array
	public	function moveElement( & $arr, $size, $k)
	{
		$head = 0;
		$tail = $size - 1;
		while ($head < $tail)
		{
			if ($arr[$head] != $k)
			{
				$head++;
			}
			else if ($arr[$tail] == $k)
			{
				$tail--;
			}
			else
			{
				// Swap the elements
				$this->swapElement($arr, $head, $tail);
				$head++;
				$tail--;
			}
		}
	}
}

function main()
{
	$task = new ShiftElements();
	// Define array of integer elements
	$arr = array(6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1);
	// Get the size of array
	$size = count($arr);
	$k = 6;
	// Display array element
	echo "\n Array elements \n";
	$task->printArray($arr, $size);
	$task->moveElement($arr, $size, $k);
	// Display array elements
	echo "\n After Move element ". $k ." to end\n";
	$task->printArray($arr, $size);
}
main();

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
/*
    Node Js Program
    Move a given element from end of arrays
*/
class ShiftElements
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Swap the elements of array by given location
	swapElement(arr, front, tail)
	{
		var temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move given element k to end of array
	moveElement(arr, size, k)
	{
		var head = 0;
		var tail = size - 1;
		while (head < tail)
		{
			if (arr[head] != k)
			{
				head++;
			}
			else if (arr[tail] == k)
			{
				tail--;
			}
			else
			{
				// Swap the elements
				this.swapElement(arr, head, tail);
				head++;
				tail--;
			}
		}
	}
}

function main()
{
	var task = new ShiftElements();
	// Define array of integer elements
	var arr = [6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1];
	// Get the size of array
	var size = arr.length;
	var k = 6;
	// Display array element
	process.stdout.write("\n Array elements \n");
	task.printArray(arr, size);
	task.moveElement(arr, size, k);
	// Display array elements
	process.stdout.write("\n After Move element " + k + " to end\n");
	task.printArray(arr, size);
}
main();

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
#  Python 3 Program
#  Move a given element from end of arrays

class ShiftElements :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Swap the elements of array by given location
	def swapElement(self, arr, front, tail) :
		temp = arr[front]
		arr[front] = arr[tail]
		arr[tail] = temp
	
	#  Move given element k to end of array
	def moveElement(self, arr, size, k) :
		head = 0
		tail = size - 1
		while (head < tail) :
			if (arr[head] != k) :
				head += 1
			
			elif(arr[tail] == k) :
				tail -= 1
			else :
				#  Swap the elements
				self.swapElement(arr, head, tail)
				head += 1
				tail -= 1
			
		
	

def main() :
	task = ShiftElements()
	#  Define array of integer elements
	arr = [6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1]
	#  Get the size of array
	size = len(arr)
	k = 6
	#  Display array element
	print("\n Array elements ")
	task.printArray(arr, size)
	task.moveElement(arr, size, k)
	#  Display array elements
	print("\n After Move element ", k ," to end")
	task.printArray(arr, size)

if __name__ == "__main__": main()

Output

 Array elements
   6   0   6   6   3   1   6   5   6   7   1

 After Move element  6  to end
   1   0   7   5   3   1   6   6   6   6   6
#  Ruby Program
#  Move a given element from end of arrays

class ShiftElements 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Swap the elements of array by given location
	def swapElement(arr, front, tail) 
		temp = arr[front]
		arr[front] = arr[tail]
		arr[tail] = temp
	end

	#  Move given element k to end of array
	def moveElement(arr, size, k) 
		head = 0
		tail = size - 1
		while (head < tail) 
			if (arr[head] != k) 
				head += 1
			elsif(arr[tail] == k) 
				tail -= 1
			else 
				#  Swap the elements
				self.swapElement(arr, head, tail)
				head += 1
				tail -= 1
			end

		end

	end

end

def main() 
	task = ShiftElements.new()
	#  Define array of integer elements
	arr = [6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1]
	#  Get the size of array
	size = arr.length
	k = 6
	#  Display array element
	print("\n Array elements \n")
	task.printArray(arr, size)
	task.moveElement(arr, size, k)
	#  Display array elements
	print("\n After Move element ", k ," to end\n")
	task.printArray(arr, size)
end

main()

Output

 Array elements 
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
/*
    Scala Program
    Move a given element from end of arrays
*/
class ShiftElements
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Swap the elements of array by given location
	def swapElement(arr: Array[Int], front: Int, tail: Int): Unit = {
		var temp: Int = arr(front);
		arr(front) = arr(tail);
		arr(tail) = temp;
	}
	// Move given element k to end of array
	def moveElement(arr: Array[Int], size: Int, k: Int): Unit = {
		var head: Int = 0;
		var tail: Int = size - 1;
		while (head < tail)
		{
			if (arr(head) != k)
			{
				head += 1;
			}
			else if (arr(tail) == k)
			{
				tail -= 1;
			}
			else
			{
				// Swap the elements
				this.swapElement(arr, head, tail);
				head += 1;
				tail -= 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ShiftElements = new ShiftElements();
		// Define array of integer elements
		var arr: Array[Int] = Array(6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1);
		// Get the size of array
		var size: Int = arr.length;
		var k: Int = 6;
		// Display array element
		print("\n Array elements \n");
		task.printArray(arr, size);
		task.moveElement(arr, size, k);
		// Display array elements
		print("\n After Move element " + k + " to end\n");
		task.printArray(arr, size);
	}
}

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6
/*
    Swift 4 Program
    Move a given element from end of arrays
*/
class ShiftElements
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Swap the elements of array by given location
	func swapElement(_ arr: inout[Int], _ front: Int, _ tail: Int)
	{
		let temp: Int = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move given element k to end of array
	func moveElement(_ arr: inout[Int], _ size: Int, _ k: Int)
	{
		var head: Int = 0;
		var tail: Int = size - 1;
		while (head < tail)
		{
			if (arr[head] != k)
			{
				head += 1;
			}
			else if (arr[tail] == k)
			{
				tail -= 1;
			}
			else
			{
				// Swap the elements
				self.swapElement(&arr, head, tail);
				head += 1;
				tail -= 1;
			}
		}
	}
}
func main()
{
	let task: ShiftElements = ShiftElements();
	// Define array of integer elements
	var arr: [Int] = [6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1];
	// Get the size of array
	let size: Int = arr.count;
	let k: Int = 6;
	// Display array element
	print("\n Array elements ");
	task.printArray(arr, size);
	task.moveElement(&arr, size, k);
	// Display array elements
	print("\n After Move element ", k ," to end");
	task.printArray(arr, size);
}
main();

Output

 Array elements
   6   0   6   6   3   1   6   5   6   7   1

 After Move element  6  to end
   1   0   7   5   3   1   6   6   6   6   6
/*
    Kotlin Program
    Move a given element from end of arrays
*/
class ShiftElements
{
	//Display elements of given array
	fun printArray(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Swap the elements of array by given location
	fun swapElement(arr: Array<Int>, front: Int, tail: Int): Unit
	{
		var temp: Int = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move given element k to end of array
	fun moveElement(arr: Array<Int>, size: Int, k: Int): Unit
	{
		var head: Int = 0;
		var tail: Int = size - 1;
		while (head<tail)
		{
			if (arr[head] != k)
			{
				head += 1;
			}
			else if (arr[tail] == k)
			{
				tail -= 1;
			}
			else
			{
				// Swap the elements
				this.swapElement(arr, head, tail);
				head += 1;
				tail -= 1;
			}
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: ShiftElements = ShiftElements();
	// Define array of integer elements
	var arr: Array<Int> = arrayOf(6, 0, 6, 6, 3, 1, 6, 5, 6, 7, 1);
	// Get the size of array
	var size: Int = arr.count();
	var k: Int = 6;
	// Display array element
	print("\n Array elements \n");
	task.printArray(arr, size);
	task.moveElement(arr, size, k);
	// Display array elements
	print("\n After Move element " + k + " to end\n");
	task.printArray(arr, size);
}

Output

 Array elements
  6  0  6  6  3  1  6  5  6  7  1

 After Move element 6 to end
  1  0  7  5  3  1  6  6  6  6  6




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