# Problem with nested for loops, Creating an array of Distinct ints from a larger array of ints.

• February 23rd, 2014, 01:15 AM
ChrisInPa
Problem with nested for loops, Creating an array of Distinct ints from a larger array of ints.
Hello all,

FNG here, I read several posts, but they were referencing the boolean method. I need to create an array of distinct ints from a larger array of ints and then print both. I figured out how to do it with the boolean return method, but I have to do it with the "public static int[] saveDistinctNumbers(int[] array)" I think I'm good except for the nested for loops at the bottom.
Thanks
Chris

This is where I'm stuck.
When I enter 1 2 3 4 1 6 3 4 5 2
It should return 1 2 3 4 6 5
It is returning 1 2 3 4 0 6 3 4 5 2

Code Java:

```import java.util.Scanner; public class FML{   public static void main(String[] args){   Scanner kbd = new Scanner(System.in); int[] array = new int[10]; int[] uniqueArray = new int[10]; System.out.println("The program will accept integers and then"+ " create \nand print an array of distinct integers.");   for(int i =0; i<array.length; i++){ System.out.print("\nEnter an integer: "); array[i] = kbd.nextInt();}   uniqueArray = saveDistinctNumbers(array);   for(int element: array){ System.out.print (element + " ");}   System.out.println ("\nDistinct");   for(int element: uniqueArray){ System.out.print (element + " ");}   }     //######################################################################################//   public static int[] saveDistinctNumbers(int[] array){   int[] temp = new int[array.length]; int counter = 0; / /###############################################//   for(int i = 0; i<array.length; i++){   for(int j = 0; j < array.length;j++){   if (array[i]!= array[j]){   temp[i] = array[i]; counter++;}   if (array[i]== array[j] && i!=j) break;}   //###############################################// } int[] uniqueArray = new int[array.length];   System.arraycopy(temp, 0, uniqueArray, 0, uniqueArray.length);   return uniqueArray; } }```
• February 23rd, 2014, 04:19 AM
GregBrannon
Re: Problem with nested for loops, Creating an array of Distinct ints from a larger array of ints.
If i were to comment your code (Why don't you?), the method saveDistinctNumbers() might be described thusly:

compares each element of the argument array to every other element of the argument array and saves in a temp array only the first occurrence of all elements - duplicates are rejected. returns the temp array of unique values when done.

If you agree that that is what the method is trying to do, then compare the comment to what you've coded and see if the implementation matches the intent.

I'm not sure what you mean by the "boolean method," but an alternative approach would be to attempt to add each element of the argument array to a temp array, only adding those values which have not already been added. That describes how a Set works.

Both approaches include the added complications of sizing the temp array and then outputting only those elements of the temp array that were added from the argument array. For example, if the temp array is of the same size as the argument array but does not contain the duplicates of the argument array, then the number of values of interest in the temp array will be something less than the number of elements in the temp array. How do you know which elements of the temp array are extra?

I'll assume that's the purpose of the 'counter' variable in your code, but it's not used to display the result. This highlights another occasion where comments that describe what your code is supposed to be doing would be helpful, possibly even indicating to you where a good thought was not completed.
• February 24th, 2014, 10:48 PM
ChrisInPa
Re: Problem with nested for loops, Creating an array of Distinct ints from a larger array of ints.
In a word yes, and I usually do comment after I'm finished.

This is what I came up with. It works fine, and will be perfect for me but surely there's a better way.

Thanks,

Chris

Code Java:

```import java.util.Scanner; public class FML{   public static void main(String[] args){   Scanner kbd = new Scanner(System.in); int[] array = new int[10]; // Test array String instr1 = "Enter 10 integers"; // instructions String instr2 = "\nInt: "; // Loop instructions String reply = "The Distinct numbers are: "; // Distinct reply     System.out.print(instr1); // instructions   for(int i =0; i<array.length; i++){ // get the ints System.out.print(instr2); array[i]=kbd.nextInt();}   int[]distinctArray = saveDistinctNumbers(array); // Call the save distinct method   System.out.println (reply); // initial reply   for(int element: distinctArray){ // Print the distinct array System.out.print (element + " ");}   }   public static int[] saveDistinctNumbers(int[] array){   boolean[] checker = new boolean [array.length]; // Bool array for finding distincts int[] temp = new int[array.length]; //temp array to pass distinct to return   for(int i = 0; i<array.length; i++){ // uses bool array to id repeat ints for(int j = 0; j < array.length;j++){ if(array[i] == array[j] && i != j) break; if(array[i]!= array[j]){ // if is not a repeat checker[i] = !checker[j];}}} // makes the index at j true   int truecount = -1; //allows temp[]to move through index at a different rate than array[] int totalcount = -1; //allows array[] to move through index at a different rate than temp[]   for (int element :array){ // for array[] totalcount++; //totalcount++ treated like i in a for loop if(checker[totalcount]){ // if checker[totalcount] is true truecount++; //truecount ++ temp[truecount] = array[totalcount];}} //filters out the spaces between repeats   int[] distinctArray = new int[truecount+1]; // properly formatted distinct array for return System.arraycopy(temp,0, distinctArray,0, distinctArray.length); //copy temp to distinct return distinctArray;} //return distinct.   }```