# 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

• 13 July

### Get current year in Java

Table of ContentsUsing Date and SimpleDateFormatUsing CalendarUsing LocalDateUsing LocalDateTimeusing ZonedDateTimeUsing OffsetDateTimeUsing Instant and ZoneIdUsing Instant and ZoneOffsetConclusion In this post, we will see how to get current year in Java. Using Date and SimpleDateFormat The Date class was introduced in Java 1.0 and provided different approaches to work with time. The SimpleDateFormat inherits from DateFormat […]

• 13 July

### Remove substring from String in Java

Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

• 13 July

### 13 Best Working 9Cartoon Alternatives in 2021 (Tried and Tested)

Table of ContentsBest 9cartoon AlternativesCartoonsOnAnimeDaoAnimeToonKissAnimeToonGetWatch Cartoons OnlineCartoonCrazyToonovaKimCartoonWatch SeriesNyaaToonJetKissCartoonFrequently Asked QuestionsWhat is 9cartoon?Is 9cartoon free?What happened to 9cartoon?Which are the best 9cartoon alternatives?Wrapping Up Are you having trouble accessing 9cartoon? Well then, you’re just at the right place. 9cartoon is a well-known web-based platform that bagged a large quality of cartoons, relative TV shows and movies. […]

• 13 July

### Einthusan Alternatives : 16 Best websites like Enthusan in 2021

Table of ContentsEinthusan alternativesYouTubeYuppTVSonyLIVJio CinemaHotstarVootZee5HungamaAmazon Prime VideoAirtel XtremeMX PlayerEros NowNetflixHonorable Mentions123 MoviesF MoviesSoap2dayFrequently Asked Questions**Is Einthusan Blocked in India?Is Einthusan Safe?Is Einthusan Free?Einthusan Downloader: Is it legit?Final Words Einthusan is a well-known platform for watching movies online. From streaming in different languages to downloading them for online viewing, the platform offered everything that movie lovers […]

• 13 July

### Everything You Need to Know About CQATest in 2021

Table of ContentsCQATest: A Brief OverviewHow Can CQATest Help?Problems Associated with CQA TestFixing Problems Associated with CQATestDisabling CQATest AppMethod 1: Force Stopping CQATest AppMethod 2: Resetting Your DevicePrecautions: Things to Keep in Mind Before Disabling CQATest.FAQsWhat is CQATest on Android?What does CQA stand for?Is CQATest app a virus?Is CQA Test Safe?What are the most common […]

• 10 July