Remove recurring digits in a given number

Here given code implementation process.

/*
    Java Program for
    Remove recurring digits in a given number
*/
public class RemoveRecurring
{
	public void removeRecurringDigit(String num)
	{
		int n = num.length();

		if (n <= 0)
		{
			// When given number is empty
			return;
		}
		System.out.println(" Given number : " + num);
		// Get first character
		char back = num.charAt(0);

		// Get first resultant character
		String result = ""+ back;

		// Execute loop through by n
		for (int i = 1; i < n; ++i)
		{
			if (back != num.charAt(i))
			{
				// When adjunct elements are not similar
				// Get new back element
				back   = num.charAt(i);
				// Append result into the last
				result = result + back;
			}
		}
		// Display calculated result
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		RemoveRecurring task = new RemoveRecurring();
		// Test input
		task.removeRecurringDigit("823382223");
		task.removeRecurringDigit("123");
		task.removeRecurringDigit("4111544222");
	}
}

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Remove recurring digits in a given number
*/
class RemoveRecurring
{
    public: void removeRecurringDigit(string num)
    {
        int n = num.length();
        if (n <= 0)
        {
            // When given number is empty
            return;
        }
        cout << " Given number : " << num << endl;
        // Get first character
        char back = num[0];
        // Get first resultant character
        string result = "";

        result += back;
        // Execute loop through by n
        for (int i = 1; i < n; ++i)
        {
            if (back != num[i])
            {
                // When adjunct elements are not similar
                // Get new back element
                back = num[i];
                // Append result into the last
                result+=back;
            }
        }
        // Display calculated result
        cout << " Result : " << result << endl;
    }
};
int main()
{
    RemoveRecurring *task = new RemoveRecurring();
    // Test input
    task->removeRecurringDigit("823382223");
    task->removeRecurringDigit("123");
    task->removeRecurringDigit("4111544222");
    return 0;
}

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
// Include namespace system
using System;
/*
    Csharp Program for
    Remove recurring digits in a given number
*/
public class RemoveRecurring
{
	public void removeRecurringDigit(String num)
	{
		int n = num.Length;
		if (n <= 0)
		{
			// When given number is empty
			return;
		}
		Console.WriteLine(" Given number : " + num);
		// Get first character
		char back = num[0];
		// Get first resultant character
		String result = "" + back;
		// Execute loop through by n
		for (int i = 1; i < n; ++i)
		{
			if (back != num[i])
			{
				// When adjunct elements are not similar
				// Get new back element
				back = num[i];
				// Append result into the last
				result = result + back;
			}
		}
		// Display calculated result
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		RemoveRecurring task = new RemoveRecurring();
		// Test input
		task.removeRecurringDigit("823382223");
		task.removeRecurringDigit("123");
		task.removeRecurringDigit("4111544222");
	}
}

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
package main
import "fmt"
/*
    Go Program for
    Remove recurring digits in a given number
*/
func removeRecurringDigit(num string) {
	var n int = len(num)
	if n <= 0 {
		// When given number is empty
		return
	}
	fmt.Println(" Given number : ", num)
	// Get first character
	var back byte = num[0]
	// Get first resultant character
	var result string = string(back)
	// Execute loop through by n
	for i := 1 ; i < n ; i++ {
		if back != num[i] {
			// When adjunct elements are not similar
			// Get new back element
			back = num[i]
			// Append result into the last
			result = result + string(back)
		}
	}
	// Display calculated result
	fmt.Println(" Result : ", result)
}
func main() {

	// Test input
	removeRecurringDigit("823382223")
	removeRecurringDigit("123")
	removeRecurringDigit("4111544222")
}

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
<?php
/*
    Php Program for
    Remove recurring digits in a given number
*/
class RemoveRecurring
{
	public	function removeRecurringDigit($num)
	{
		$n = strlen($num);
		if ($n <= 0)
		{
			// When given number is empty
			return;
		}
		echo(" Given number : ".$num."\n");
		// Get first character
		$back = $num[0];
		// Get first resultant character
		$result = "".strval($back);
		// Execute loop through by n
		for ($i = 1; $i < $n; ++$i)
		{
			if ($back != $num[$i])
			{
				// When adjunct elements are not similar
				// Get new back element
				$back = $num[$i];
				// Append result into the last
				$result = $result.strval($back);
			}
		}
		// Display calculated result
		echo(" Result : ".$result."\n");
	}
}

function main()
{
	$task = new RemoveRecurring();
	// Test input
	$task->removeRecurringDigit("823382223");
	$task->removeRecurringDigit("123");
	$task->removeRecurringDigit("4111544222");
}
main();

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
/*
    Node JS Program for
    Remove recurring digits in a given number
*/
class RemoveRecurring
{
	removeRecurringDigit(num)
	{
		var n = num.length;
		if (n <= 0)
		{
			// When given number is empty
			return;
		}
		console.log(" Given number : " + num);
		// Get first character
		var back = num.charAt(0);
		// Get first resultant character
		var result = "" + back;
		// Execute loop through by n
		for (var i = 1; i < n; ++i)
		{
			if (back != num.charAt(i))
			{
				// When adjunct elements are not similar
				// Get new back element
				back = num.charAt(i);
				// Append result into the last
				result = result + back;
			}
		}
		// Display calculated result
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new RemoveRecurring();
	// Test input
	task.removeRecurringDigit("823382223");
	task.removeRecurringDigit("123");
	task.removeRecurringDigit("4111544222");
}
main();

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
#    Python 3 Program for
#    Remove recurring digits in a given number
class RemoveRecurring :
	def removeRecurringDigit(self, num) :
		n = len(num)
		if (n <= 0) :
			#  When given number is empty
			return
		
		print(" Given number : ", num)
		#  Get first character
		back = num[0]
		#  Get first resultant character
		result = ""+ str(back)
		i = 1
		#  Execute loop through by n
		while (i < n) :
			if (back != num[i]) :
				#  When adjunct elements are not similar
				#  Get new back element
				back = num[i]
				#  Append result into the last
				result = result + str(back)
			
			i += 1
		
		#  Display calculated result
		print(" Result : ", result)
	

def main() :
	task = RemoveRecurring()
	#  Test input
	task.removeRecurringDigit("823382223")
	task.removeRecurringDigit("123")
	task.removeRecurringDigit("4111544222")

if __name__ == "__main__": main()

Output

 Given number :  823382223
 Result :  823823
 Given number :  123
 Result :  123
 Given number :  4111544222
 Result :  41542
#    Ruby Program for
#    Remove recurring digits in a given number
class RemoveRecurring 
	def removeRecurringDigit(num) 
		n = num.length
		if (n <= 0) 
			#  When given number is empty
			return
		end

		print(" Given number : ", num, "\n")
		#  Get first character
		back = num[0]
		#  Get first resultant character
		result = "" + back.to_s
		i = 1
		#  Execute loop through by n
		while (i < n) 
			if (back != num[i]) 
				#  When adjunct elements are not similar
				#  Get new back element
				back = num[i]
				#  Append result into the last
				result = result + back.to_s
			end

			i += 1
		end

		#  Display calculated result
		print(" Result : ", result, "\n")
	end

end

def main() 
	task = RemoveRecurring.new()
	#  Test input
	task.removeRecurringDigit("823382223")
	task.removeRecurringDigit("123")
	task.removeRecurringDigit("4111544222")
end

main()

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
/*
    Scala Program for
    Remove recurring digits in a given number
*/
class RemoveRecurring()
{
	def removeRecurringDigit(num: String): Unit = {
		var n: Int = num.length();
		if (n <= 0)
		{
			// When given number is empty
			return;
		}
		println(" Given number : " + num);
		// Get first character
		var back: Char = num.charAt(0);
		// Get first resultant character
		var result: String = "" + back.toString();
		var i: Int = 1;
		// Execute loop through by n
		while (i < n)
		{
			if (back != num.charAt(i))
			{
				// When adjunct elements are not similar
				// Get new back element
				back = num.charAt(i);
				// Append result into the last
				result = result + back.toString();
			}
			i += 1;
		}
		// Display calculated result
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: RemoveRecurring = new RemoveRecurring();
		// Test input
		task.removeRecurringDigit("823382223");
		task.removeRecurringDigit("123");
		task.removeRecurringDigit("4111544222");
	}
}

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542
import Foundation;
/*
    Swift 4 Program for
    Remove recurring digits in a given number
*/
class RemoveRecurring
{
	func removeRecurringDigit(_ text: String)
	{
      	let num = Array(text);
		let n: Int = num.count;
		if (n <= 0)
		{
			// When given number is empty
			return;
		}
		print(" Given number : ", text);
		// Get first character
		var back: Character = num[0];
		// Get first resultant character
		var result: String = String(back);
		var i: Int = 1;
		// Execute loop through by n
		while (i < n)
		{
			if (back  != num[i])
			{
				// When adjunct elements are not similar
				// Get new back element
				back = num[i];
				// Append result into the last
				result = result + String(back);
			}
			i += 1;
		}
		// Display calculated result
		print(" Result : ", result);
	}
}
func main()
{
	let task: RemoveRecurring = RemoveRecurring();
	// Test input
	task.removeRecurringDigit("823382223");
	task.removeRecurringDigit("123");
	task.removeRecurringDigit("4111544222");
}
main();

Output

 Given number :  823382223
 Result :  823823
 Given number :  123
 Result :  123
 Given number :  4111544222
 Result :  41542
/*
    Kotlin Program for
    Remove recurring digits in a given number
*/
class RemoveRecurring
{
	fun removeRecurringDigit(num: String): Unit
	{
		val n: Int = num.length;
		if (n <= 0)
		{
			// When given number is empty
			return;
		}
		println(" Given number : " + num);
		// Get first character
		var back: Char = num.get(0);
		// Get first resultant character
		var result: String = "" + back.toString();
		var i: Int = 1;
		// Execute loop through by n
		while (i < n)
		{
			if (back != num.get(i))
			{
				// When adjunct elements are not similar
				// Get new back element
				back = num.get(i);
				// Append result into the last
				result = result + back.toString();
			}
			i += 1;
		}
		// Display calculated result
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: RemoveRecurring = RemoveRecurring();
	// Test input
	task.removeRecurringDigit("823382223");
	task.removeRecurringDigit("123");
	task.removeRecurringDigit("4111544222");
}

Output

 Given number : 823382223
 Result : 823823
 Given number : 123
 Result : 123
 Given number : 4111544222
 Result : 41542


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