Created
March 1, 2019 12:41
-
-
Save master-elodin/e469063dae34a0820d3f3c6e7e408561 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 | |
32 | |
65 | |
32 | |
99 | |
12 | |
77 | |
15 | |
56 | |
40 | |
54 | |
39 | |
62 | |
32 | |
35 | |
44 | |
78 | |
98 | |
12 | |
55 | |
60 | |
77 | |
16 | |
25 | |
52 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class Sort { | |
private static final int MAX_NUMBERS = 25; | |
private static final int MAX_VALUE = 200; | |
public static void main(String[] args) throws FileNotFoundException { | |
Scanner fileScan = new Scanner(new File("numbers.dat")); | |
// since we've got enough space in our array for any possible number from the file, | |
// we only care if the number exists or not | |
// We don't actually care about number of duplicates since it'll be (MAX_NUMBERS - num true in the boolean array) | |
boolean[] nonDuplicateValuesPerIndex = new boolean[MAX_VALUE]; | |
for(int i = 0; i < MAX_NUMBERS; i++) { | |
nonDuplicateValuesPerIndex[fileScan.nextInt()] = true; | |
} | |
// now we create our actual array that we'll return | |
int[] sortedNonDuplicateNumbers = new int[MAX_NUMBERS]; | |
// since we're going in order of the boolean array, it's already sorted | |
int sortedNumberIndex = 0; | |
for(int i = 0; i < MAX_VALUE; i++) { | |
if(nonDuplicateValuesPerIndex[i]){ | |
sortedNonDuplicateNumbers[sortedNumberIndex++] = i; | |
} | |
} | |
// now add zeros to the end | |
for(int i = sortedNumberIndex; i < MAX_NUMBERS; i++) { | |
sortedNonDuplicateNumbers[i] = 0; | |
} | |
// now print each out on its own line | |
for(int i = 0; i < MAX_NUMBERS; i++) { | |
System.out.println(sortedNonDuplicateNumbers[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment