# Permutations of array in java

If you want to practice data structure and algorithm programs, you can go through Java coding interview questions.

In this post, we will see how to find all permutations of the array in java.

## Problem 1

Given array of distinct integers, print all permutations of the array.

For example:

array : [10, 20, 30]

Permuations are :

[10, 20, 30] [10, 30, 20] [20, 10, 30] [20, 30, 10] [30, 10, 20] [30, 20, 10]

## Solution

We can solve the problem with the help of recursion. It is quite hard to explain recursion, so I have created a recursion tree to demonstrate it.

Here is the code for the same.

When you run above program, you will get below output:

Permuations of array : [10, 20, 30] are:
=========================================
[10, 20, 30] [10, 30, 20] [20, 10, 30] [20, 30, 10] [30, 10, 20] [30, 20, 10]

I have illustrated how recursion is working here with below diagram.

You need to open this diagram in new window and zoom it.

As we have 3 elements in the array, that’s why we have 3 branches for each node.

## Problem 2

Given array of integers(can contain duplicates), print all permutations of the array.

## Solution

We can solve this using recursion as well but need to take care of duplicates.We will sort the array, so all duplicates will be conitguous.

Permuations of array : [10, 20, 10] are:
=========================================
[10, 10, 20] [10, 20, 10] [20, 10, 10]

That’s all about Permutations of array in java.

import_contacts

## Related Posts

• 03 May

### Make requirements.txt in python

Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

• 03 May

### Create an Array of 1 to 10 in Python

Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

• 03 May

### Remove Urls from Text in Python

Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

• 03 May

### Create a List from 1 to 100 in Python

Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

• 23 April

### How to initialize an Array in Constructor in C++

Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

• 23 April

### Set an Array Equal to Another Array in Java

Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.