Java Removing Redundant Items in Array - java

For this particular problem I am attempting to remove redundant elements in an sorted array and replace them all with 0s at the end of the array. For example, if I had an array consisting of the int elements
1,3,3,4,4,5,6,6,7
My output array should be
1,3,4,5,6,7,0,0,0
My first attempt at the problem was to create a swapper in order to push all the 0s to the end of the list after removing the elements, but it won't seem to push the zeros to the end of the list. Here is my code.
public void implode(int[] ary)
{
int swapper = -1;
int[] newARY = new int[ary.length];
int current = -1;
for (int i = 0; i < ary.length; i++)
{
if (current != ary[i])
{
newARY[i] = ary[i];
current = ary[i];
}
}
for (int i = 0; i < ary.length; i++)
{
if (ary[i] == 0)
{
if (ary[i + 1] != 0)
{
swapper = ary[i + 1];
ary[i] = swapper;
ary[i + 1] = 0;
}
}
}
ary = newARY;
for (int i = 0; i < newARY.length; i++)
{
System.out.print(newARY[i] + " ");
}
}
The array im testing it with is,
int[] aryIn2 = {1, 1, 2, 3, 4, 4, 5, 6};
However, when outputting the imploded array, I receive this one.
1 0 2 3 4 0 5 6
Is there something I am missing?
Thanks in advance.

not an answer to your problem, but using (if possible) java streams can shorten your way:
int[] arr = {1,3,3,4,4,5,6,6,7};
// distinct
List<Integer> list = Arrays.stream(arr).distinct().boxed().collect(Collectors.toList());
// pad with zero's
while(list.size() < arr.length) {
list.add(0);
}
// display
System.out.println(list.stream().map(String::valueOf).collect(Collectors.joining(",")));
will output
1,3,4,5,6,7,0,0,0

Two issue with you code that I observed.
1) Your swapper logic is performing swapping on a different array than the one in which you had done modification earlier
2) You need to have this logic in a bubble-sort way, i.e. loop inside a loop
Below is a working modified sample code of your method. I have modified only the second for-loop logic
public void implode(int[] ary) {
int swapper = -1;
int[] newARY = new int[ary.length];
int current = -1;
for (int i = 0; i < ary.length; i++) {
if (current != ary[i]) {
newARY[i] = ary[i];
current = ary[i];
}
}
for (int i = 0; i < newARY.length - 1; i++) {
if (newARY[i] == 0 && newARY[i + 1] != 0) {
for (int j = i; (j + 1) < newARY.length; j++) {
swapper = newARY[j + 1];
newARY[j] = swapper;
newARY[j + 1] = 0;
}
}
}
for (int i = 0; i < newARY.length; i++) {
System.out.print(newARY[i] + " ");
}
}

In this first loop:
for (int i = 0; i < ary.length; i++) {
if (current != ary[i]) {
newARY[i] = ary[i];
current = ary[i];
}
}
You fill newARY with elements in ary with duplicated value turns to 0:
newARY: 1 0 2 3 4 0 5 6
However, in the second loop:
for (int i = 0; i < ary.length; i++)
{
if (ary[i] == 0)
{
if (ary[i + 1] != 0)
{
swapper = ary[i + 1];
ary[i] = swapper;
ary[i + 1] = 0;
}
}
}
You're modifying your original ary array. So the newARY is not updated.
However, your attempt to push 0 to the end of array also fail if there are more than two 0s consecutive. And it is also vulnerable to ArrayOutOfBoundIndexException since you try to read ary[i+1] without restriction on i
One simple and straight forward way to push 0s to the end of the array is to create new array with non-0s elements and fill 0s later:
int[] result = new int[ary.lenght];
int resultIndex = 0;
for (int i = 0; i < newARY.length; i++) {
if (newARY[i] != 0) {
result[resultIndex++] = newAry[i];
}
}
for (int i = resultIndex; i < newARY.length; i++) {
result[i] = 0;
}
// Print result array
Hint: Using above strategy, you can simplify your code. No need to create immediate array newARY. Just loop over the original array, push unique elements to the result array, then fill any slot left with 0s.

Related

A java Program which take input from 1 to 9 and print all odd from left to right then print even from right to left

I want only one loop to archive this output
input={1,2,3,4,5,6,7,8,9} output={1,3,5,7,9,8,6,4,2}
public static void printOddEven(int[] arr) {
int newArray[] = new int[10];
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
newArray[i] = arr[i];
System.out.print(newArray[i] + " ");
}
}
for (int i = arr.length - 1; i > 0; i--) {
if (arr[i] % 2 == 0) {
newArray[i] = arr[i];
System.out.print(newArray[i] + " ");
}
}
}
If you want to use an array:
int [] result = new int[arr.length];
int counterFront = 0;
int counterBack = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
result[counterFront++] = arr[i];
}
if (arr[i] % 2 == 0) {
result[counterBack--] = arr[i];
}
}
return result;
EDIT: Thanks to a comment, found out it had a ArrayIndexOutOfBounds.
int newArray[] = new int[9];
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0)
newArray[i/2] = arr[i];
else
newArray[8-(i/2)] = arr[i];
}
System.out.println (java.util.Arrays.toString (newArray));
Just use a descendant index from the right
Why do you use Arrays at all? Is it homework? Note that you get an off-by-one-error, because your newArray is too large, when using int[10] for 9 elements, a typical problem with Arrays.
I reckon this is more of a maths problem than a programming problem. It's about knowing there is a simple arithmetic relationship between an incrementing index and a decrementing index.
int[] arr = {1,2,3,4,5,6,7,8,9};
public static void printOddEven(int[] arr) {
int[] odds = new int[5]; // arr.length == 9
int[] even = new int[4];
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
// This is where the magic happens
// It is filling the array from the back
even[even.length - (i / 2) - 1] = arr[i];
} else {
odds[(i / 2)] = arr[i];
}
}
System.out.println(java.util.Arrays.toString(odds));
System.out.println(java.util.Arrays.toString(even));
}
EDIT:
Just for #CoderinoJavarino, here is a version where the output is a single array. The core logic and maths is identical, so take your pick which is easier to understand.
The use of Arrays.toString() is not there as part of the algorithm solution. It is there simply so that you can see the output. I could equally send the output to a file, or to a web socket.
The output is not the printing, the output is the array or arrays. It could equally have been a List, or a special class just for sorting odd/even numbers. Who cares?
In industrial programming (ie, non-academic) this is how code gets divided up: for ease of understanding, not cleverness. And in the business world there is no concept of "cheating": Nobody worries about the internals of, say, a JSP, rendering your array to a browser.
int[] arr = {1,2,3,4,5,6,7,8,9};
public static int[] SORTOddEven(int[] arr) {
int[] output = new int[arr.length]; // arr.length == 9
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
// This is where the magic happens
// It is filling the array from the back
output[output.length - (i / 2) - 1] = arr[i];
} else {
output[(i / 2)] = arr[i];
}
}
return output;
}
System.out.println(java.util.Arrays.toString(SORTOddEven(arr)));
public static void printOddEven(int[] arr) {
int newArray[] = new int[9];
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0)
newArray[i/2] = arr[i];
else
newArray[arr.length - i/2 - 1] = arr[i];
}
System.out.println(java.util.Arrays.toString(newArray));
}
Live on Ideone.
This only works for 123456789 to print 135798642:
public class Sample {
public static void main(String[] args) throws Exception {
int j=0;
int p=2;
int newArray[]= {1,2,3,4,5,6,7,8,9};
for(int i=0;i<=newArray.length-1;i++)
{
if(i<=4)
{
System.out.print(newArray[i]+j);
j++;
}
else
{
System.err.print(newArray[i]+p);
p=p-3;
}
}
}
}

Finding consecutive elements of an array using Java

I'm almost new at Java! I want to control if there are 4 consecutive elements in an array with 5 elements. Is there any way to do that? Can someone help me with that? Thanks! Consecutive like {2, 3, 4, 5}. If there is {3, 4, 2, 5} for example this is not consecutive.I want just a simple example if someone can help me.
I did this but I think this is incorrect:
public int katerTeNjepasnjeshme()
{
int[] numrat=new int[zar.length];
for(int i=0;i<zar.length;i++)
numrat[i]=zar[i].getValue();
int shuma=0;
for(int i=0;i<zar.length-1;i++)
{
if(zar[i+1].getValue()==(zar[i].getValue()+1))
Joptionpane.showMessageDialog(null,"Tere are cons elements");
}
Here's the general idea:
Keep a counter (initialized appropriately), to keep track of the number of consecutive elements as you iterate over the elements.
If the counter reaches 4, you have found 4 consecutive elements.
If you encounter an element that is not consecutive, then reset the counter to 1, and proceed to check the next element.
Here is a sample code snippet:
public static void findConsecutive()
{
int[] array = {1,2,3,5,6,7,8,10};
int counter = 1;
int i = 1;
for (i = 1; i < array.length; i++)
{
if (array[i] == (array[i-1] + 1))
{
counter++;
if (counter == 4)
{
System.out.println("Consecutive elements are at array index: " + (i - 3) + " to " + i);
break;
}
}
else
{
counter = 1;
}
}
}
i think something like that should work:
int[] mylist = new int[10];
for (int i = 0; i < myList.length; i++) {
int k = 1;
for (int j = 1; j < 5 j++) {
if (mylist[i] == mylist[i+j]-j) {
k++;
}
if (k=5) System.out.println("found");
}
}
As soon as you need to compare two elements of the array you should use proper bounds in the loop, otherwise you will get ArrayIndexOutOfBoundsException
boolean consequtive = true;
for (int i = 0; i < zar.length - 1; i++)
if (zar[i+1].getValue() != zar[i].getValue() + 1) {
consecutive = false;
break;
}
if (consequtive)
Joptionpane.showMessageDialog(null,"Tere are cons elements");

Getting the most "popular" number from array

I need for homework to get the most "popular" number in an array (the number in the highest frequency), and if there are several numbers with the same number of shows, get some number randomly.
After more then three hours of trying, and either searching the web, this is what I got:
public int getPopularNumber(){
int count = 1, tempCount;
int popular = array[0];
int temp = 0;
for ( int i = 0; i < (array.length - 1); i++ ){
if ( _buses[i] != null )
temp = array[i];
tempCount = 0;
for ( int j = 1; j < _buses.length; j++ ){
if ( array[j] != null && temp == array[j] )
tempCount++;
}
if ( tempCount > count ){
popular = temp;
count = tempCount;
}
}
return popular;
}
This code work, but don't take into account an important case- if there is more than one number with the same count of shows. Then it just get the first one.
for example: int[]a = {1, 2, 3, 4, 4, ,5 ,4 ,5 ,5}; The code will grab 4 since it shown first, and it's not random as it should be.
Another thing- since it's homework I can't use ArrayList/maps and stuff that we still didn't learn.
Any help would be appreciated.
Since they didn't give you any time complexity boundary, you can "brute force" the problem by scanning the the array N^2 times. (disclaimer, this is the most intuitive way of doing it, not the fastest or the most efficient in terms of memory and cpu).
Here is some psuedo-code:
Create another array with the same size as the original array, this will be the "occurrence array"
Zero its elements
For each index i in the original array, iterate the original array, and increment the element in the occurrence array at i each time the scan finds duplicates of the value stored in i in the original array.
Find the maximum in the occurrence array
Return the value stored in that index in the original array
This way you mimic the use of maps with just another array.
If you are not allowed to use collection then you can try below code :
public int getPopularNumber(){
int inputArr[] = {1, 2, 3, 4, 4, 5 ,4 ,5 ,5}; // given input array
int[] tempArr = new int[inputArr.length];
int[] maxValArr = new int[inputArr.length];
// tempArr will have number as index and count as no of occurrence
for( int i = 0 ; i < inputArr.length ; i++){
tempArr[inputArr[i]]++;
}
int maValue = 0;
// find out max count of occurrence (in this case 3 for value 4 and 5)
for( int j = 0 ; j < tempArr.length ; j++){
maValue = Math.max(maValue, tempArr[j]);
}
int l =0;
// maxValArr contains all value having maximum occurrence (in this case 4 and 5)
for( int k = 0 ; k < tempArr.length ; k++){
if(tempArr[k] == maValue){
maxValArr[l] = k;
l++;
}
}
return maxValArr[(int)(Math.random() * getArraySize(maxValArr))];
}
private int getArraySize(int[] arr) {
int size = 0;
for( int i =0; i < arr.length ; i++){
if(arr[i] == 0){
break;
}
size++;
}
return size;
}
that's hard as hell :D
After some trying, I guess I have it (If there will be 2 numbers with same frequency, it will return first found):
int mostPopNumber =0;
int tmpLastCount =0;
for (int i = 0; i < array.length-1; i++) {
int tmpActual = array[i];
int tmpCount=0;
for (int j = 0; j < array.length; j++) {
if(tmpActual == array[j]){
tmpCount++;
}
}
// >= for the last one
if(tmpCount > tmpLastCount){
tmpLastCount = tmpCount;
mostPopNumber = tmpActual;
}
}
return mostPopNumber;
--
Hah your code give me idea- you cant just remember last most popular number, btw I've found it solved there Find the most popular element in int[] array
:)
EDIT- after many, and many years :D, that works well :)
I've used 2D int and Integer array - you can also use just int array, but you will have to make more length array and copy actual values, Integer has default value null, so that's faster
Enjoy
public static void main(String[] args) {
//income array
int[] array= {1,1,1,1,50,10,20,20,2,2,2,2,20,20};
//associated unique numbers with frequency
int[][] uniQFreqArr = getUniqValues(array);
//print uniq numbers with it's frequency
for (int i = 0; i < uniQFreqArr.length; i++) {
System.out.println("Number: " + uniQFreqArr[i][0] + " found : " + uniQFreqArr[i][1]);
}
//get just most frequency founded numbers
int[][] maxFreqArray = getMaxFreqArray(uniQFreqArr);
//print just most frequency founded numbers
System.out.println("Most freq. values");
for (int i = 0; i < maxFreqArray.length; i++) {
System.out.println("Number: " + maxFreqArray[i][0] + " found : " + maxFreqArray[i][1]);
}
//get some of found values and print
int[] result = getRandomResult(maxFreqArray);
System.out.println("Found most frequency number: " + result[0] + " with count: " + result[1]);
}
//get associated array with unique numbers and it's frequency
static int[][] getUniqValues(int[] inArray){
//first time sort array
Arrays.sort(inArray);
//default value is null, not zero as in int (used bellow)
Integer[][] uniqArr = new Integer[inArray.length][2];
//counter and temp variable
int currUniqNumbers=1;
int actualNum = inArray[currUniqNumbers-1];
uniqArr[currUniqNumbers-1][0]=currUniqNumbers;
uniqArr[currUniqNumbers-1][1]=1;
for (int i = 1; i < inArray.length; i++) {
if(actualNum != inArray[i]){
uniqArr[currUniqNumbers][0]=inArray[i];
uniqArr[currUniqNumbers][1]=1;
actualNum = inArray[i];
currUniqNumbers++;
}else{
uniqArr[currUniqNumbers-1][1]++;
}
}
//get correctly lengthed array
int[][] ret = new int[currUniqNumbers][2];
for (int i = 0; i < uniqArr.length; i++) {
if(uniqArr[i][0] != null){
ret[i][0] = uniqArr[i][0];
ret[i][1] = uniqArr[i][1];
}else{
break;
}
}
return ret;
}
//found and return most frequency numbers
static int[][] getMaxFreqArray(int[][] inArray){
int maxFreq =0;
int foundedMaxValues = 0;
//filter- used sorted array, so you can decision about actual and next value from array
for (int i = 0; i < inArray.length; i++) {
if(inArray[i][1] > maxFreq){
maxFreq = inArray[i][1];
foundedMaxValues=1;
}else if(inArray[i][1] == maxFreq){
foundedMaxValues++;
}
}
//and again copy to correctly lengthed array
int[][] mostFreqArr = new int[foundedMaxValues][2];
int inArr= 0;
for (int i = 0; i < inArray.length; i++) {
if(inArray[i][1] == maxFreq){
mostFreqArr[inArr][0] = inArray[i][0];
mostFreqArr[inArr][1] = inArray[i][1];
inArr++;
}
}
return mostFreqArr;
}
//generate number from interval and get result value and it's frequency
static int[] getRandomResult(int[][] inArray){
int[]ret=new int[2];
int random = new Random().nextInt(inArray.length);
ret[0] = inArray[random][0];
ret[1] = inArray[random][1];
return ret;
}

Trying to show all permutations possible without recursion

I'm trying to show all combinations possible without using recursion.
I was trying it with a loop but it isn't working.
Without recursion(Not Working):
import java.util.Arrays;
public class Combination {
public static void main(String[] args) {
String[] arr = {"A","B","C","D","E","F"};
String[] result = new String[3];
int i = 0, len = 3;
while(len != 0 || i <= arr.length-len)
{
result[result.length - len] = arr[i];
len--;
i++;
}
if (len == 0){
System.out.println(Arrays.toString(result));
return;
}
}
}
With recursion(Working):
import java.util.Arrays;
public class Combination {
public static void main(String[] args){
String[] arr = {"A","B","C","D","E","F"};
combinations2(arr, 3, 0, new String[3]);
}
static void combinations2(String[] arr, int len, int startPosition, String[] result){
if (len == 0){
System.out.println(Arrays.toString(result));
return;
}
for (int i = startPosition; i <= arr.length-len; i++){
result[result.length - len] = arr[i];
combinations2(arr, len-1, i+1, result);
}
}
}
What am I doing wrong?
It is not clear what is the answer you are trying to get. The question should be set with example data to specify exactly what is it that you want to get. Given the part of the program you provided. I guess what you are looking for is all possible subsets.
the following code will get you all the possible subsets. You can easily modify this program to give you all possible subsets of a specific size (Try it to make sure you understand how it works).
static void subsets(String[]arr) {
int len = arr.length;
for (int i = 0; i < (1 << len); i++) { // 1 << len means 2 ^ len there are 2 ^ n subsets for a set of size n
/*
* We will use the bits of the integer i to represent which elements are taken
* a bit of value 1 means this element is included in the current subset
* a bit of value 0 means that we do not take the element in the given position
*/
String res = "[";
for(int j = 0; j < len; j++) { //loop over the bits of i from 0 to len -1
if ( (i & (1 << j)) != 0) {
//the jth bit is 1 in the integer i as such the jth
//element is taken in the current subset
res += (arr[j]+", ");
}
}
res += "]";
int lastCommaPosition = res.lastIndexOf(',');
if (res.lastIndexOf(',') != -1) { // a comma exists
res = res.substring(0, lastCommaPosition)+ "]";//remove the extra comma and space
}
System.out.println(res);
}
}
You can modify the method above to take an integer size for example, count the number of bits of value 1 (the number of elements taken) if this count is equal to the size you print it otherwise you don't. Feel free to modify the question if this is not the answer you wanted or ask in the comments if there is something you don't understand.
Here is how you can get the exact output without recursion. I am using for loops though:
for(int i = 0; i < arr.length - 2; i ++) {
for(int j = i + 1; j < arr.length - 1; j++ ) {
for(int k = j + 1; k < arr.length ; k++ ) {
System.out.println(arr[i] + ", " + arr[j] + ", " + arr[k]);
}
}
}

Completely stumped on a multiple loop Java program

The following is NOT a homework problem, it's just a set of problems that I've been working through for practice and I was wondering if anybody else could figure it out:
http://codingbat.com/prob/p159339
Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.
*SOLVED - here is my working code:
public int[] fix34(int...nums)
{
int[] returnArray = new int[nums.length];
//ASSIGN ARRAY
//We know that all 3's can't be moved, and after every 3 there
//will automatically be a 4
for(int i = 0; i<nums.length; i++)
{
if(nums[i] == 3)
{
returnArray[i] = 3;
returnArray[i+1] = 4;
}
}
//REBUILD ARRAY - UNMOVED INDEXES
//If a value was not moved/affected by the above, it will get placed into the array
//in the same position
for (int i = 0; i < nums.length; i++)
{
if (returnArray[i] != 3 && returnArray[i] != 4 && nums[i] != 3 && nums[i] != 4)
{
returnArray[i] = nums[i];
}
}
//REBUILD ARRAY - MOVED INDEXES
//changed values = 0 in returnArray, as a result, any time we hit a 0 we
//can simply assign the value that was in the 4's place in the nums array
OuterLoop: for (int i = 0; i < nums.length; i++)
{
if (returnArray[i] == 0)
{
for (int n = 0; n < returnArray.length; n++)
{
if (returnArray[n] == 4)
{
returnArray[i] = nums[n];
continue OuterLoop;
}
}
}
}
return returnArray;
}
I don't know java, but maybe I can help anyway. i dont want to give you the solution, but think of it like this:
you can move every number that isn't a 3. that's our only limit. that being said:
the only spots you need to change are the spots following 3s....so....every time you loop through, your program should be aware if it finds a spot after a 3 that isn't a 4....
it should also be aware if it finds any 4s not preceded by a 3......
during each loop, once it's found the location of each of those two things, you should know what to do.
Initialize all the variables
for(int i = 0; i<n-1; i++)
{
if(arr[i] == 3)
{
if(arr[i+1] == 4)
continue;
else
{
temp = 0;
while(arr[temp] != 4)
temp++;
//Write your own code here
}
//Complete the code
}
I have NOT provided the entire code. Try completing it as you said it was for your practice.
public int[] fix34(int[] nums) {
int[] arr = new int[nums.length];
int index = 0;
int tempVal= 0,j=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==3){
arr[i] = nums[i];
index=i+1;
tempVal = nums[i+1];
j=index;
while(j<nums.length){
if(j<nums.length && nums[j]==4){
//System.out.println(j+"\t="+nums[j]);
nums[j]=tempVal;
nums[index] = 4;
break;
}
j++;
}
tempVal=0;
index=0;
}else{
arr[i] = nums[i];
}
}
index =0;
for(int i=0;i<nums.length;i++){
if(nums[i]==3 && nums[i+1]==4){
i+=1;
}else if(nums[i]==4){
index = i;
j=index;
while(j<nums.length){
if(nums[j]==3 && nums[j+1]!=4){
arr[index] = nums[j+1];
arr[j+1] = 4;
}
j++;
}
}
}
return arr;
}
Here's mine: A little overkill, but is always right, anyways i make 2 additional arrays and I make 2 passes in the loop putting the correct elements in the correct places. See Logic Below.
public int[] fix34(int[] nums) {
int index1 = 0;
int index2 = 0;
int index3 = 0;
int[] only4 = fours(nums); //holds all 4's in nums
int[] misc = new int[count4(nums)]; //will hold numbers after 3
for(int a = 0; a < nums.length - 1; a++){
if(nums[a] == 3){
misc[index1] = nums[a + 1]; //get it for later use
index1++;
nums[a + 1] = only4[index2]; //now the number after 3 is a 4, from the
index2++; //only4 array
}
}
for(int b = 1; b < nums.length; b++){
if(nums[b] == 4 && nums[b - 1] != 3){ //finds misplaced 4's
nums[b] = misc[index3]; //replaces lone 4's with the
index3++; //right hand side of each 3 original values.
}
}
return nums;
}
public int count4(int[] nums){
int cnt = 0;
for(int e : nums){
if(e == 4){
cnt++;
}
}
return cnt;
}
public int[] fours(int[] nums){
int index = 0;
int[] onlyFours = new int[count4(nums)]; //must set length
for(int e : nums){
if(e == 4){
onlyFours[index] = e;
index++;
}
}
return onlyFours;
}
I solved mine using two ArrayLists which contain the places of 3's and 4's.
I hope this helps.
public int[] fix34(int[] nums)
{
//Create a copy of nums to manipulate.
int[] ret = nums;
//Create two ArrayLists which carry corresponding places of 3 and 4;
ArrayList<Integer> threePositions = new ArrayList<Integer>();
ArrayList<Integer> fourPositions = new ArrayList<Integer>();
//Get the places of 3 and 4 and put them in the respective ArrayLists.
for (int i = 0; i < ret.length; i++)
{
if (ret[i] == 3)
{
threePositions.add(i);
}
if (ret[i] == 4)
{
fourPositions.add(i);
}
}
//Swap all ints right after the 3 with one of the 4s by using the referenced
//ArrayLists values.
for (int i = 0; i < threePositions.size(); i++)
{
int temp = ret[threePositions.get(i) + 1];
ret[threePositions.get(i) + 1] = ret[fourPositions.get(i)];
ret[fourPositions.get(i)] = temp;
}
//Return the ret array.
return ret;
}

Categories