In Java, the convention wants that methods are written in camelCase. Which means PrintArray
should be printArray
and so on.
The indentation is a little off with the declaration of the int[]
. Instead of this :
int []fArrayDuplicate
It should be like this
int[] fArrayDuplicate
It might look like nitpicking (and it might be!) but it improves readability by a ton.
Talking about nitpicking, I think your for
loop could use some spacing :
for(int x=0;x<len;x++)
could be
for (int x = 0; x < len; x++)
When you comment your code, you should explain why you wrote what you did write, not what you wrote. I mean,
int len = finalArray.length; //First, I took the length of the array.
is pretty obvious, so the comment isn't neccessary. I think it is the same for all your comments, except the third. But what I need to understand, as a developper who reads your code, isn't that you used a formula to get the total, but to understand how your formula works so you can get the total.
Both your methods use the same int[]
, which makes me think it should be a parameter of both your methods, this way you can be sure they will never be different.
public static void main(String[] args){
int[] fArrayDuplicate = {1,2,3,4,5,7,8,9,10};
PrintArray(fArrayDuplicate);
FindMissing(fArrayDuplicate);
}
I also think you should pull out the main
method of this class and put it in a class that is used only to start the application with the good parameters. Doing so, you could use your MissingInteger
class to only find missing integer. This way, if you ever want to reuse it, you will be able to without bringing the main
method with it.
Also, you could make your MissingInteger
class respect the OOP more. You could input the int[]
in the constructor like so :
public class MissingInteger {
private int[] fArrayDuplicate;
public MissingInteger(int[] fArrayDuplicate){
this.fArrayDuplicate = fArrayDuplicate;
}
public void PrintArray(){
System.out.println("\nGiven the incomplete array:");
System.out.print("[");
for(int a = 0 ; a<fArrayDuplicate.length ; a++)
System.out.print(" " + fArrayDuplicate[a] + " ");
System.out.print("]");
}
public void FindMissing(){
int len = fArrayDuplicate.length; //First, I took the length of the array.
int sum = 0;
for(int x=0;x<len;x++)
sum+=fArrayDuplicate[x]; //Second, I took the sum of the given array.
int totalNumber = ((len + 1) * (len + 2)) / 2; //Third, I used this formula to get the total number.
int missingInt = totalNumber - sum; // Fourth, I subtract the sum to total number to get missing integer.
System.out.println("\nTherefore, the missing integer is " + missingInt + ".");
}
}
I think you might want to rename your class to MissingIntegerFinder
since well, the class itself isn't about a missing integer, but about finding a missing integer. And the name fArrayDuplicate doesn't mean much to me. There's nothing talking about duplicates in your code, why is it prefixed with a f? I have a hard time to find a better name, someone might find one. But for now I'd name it integersWithOneMissing
or... something like that.
fArrayDuplicate[i]
tofArrayDuplicate[i + 1]
. If the difference is two, then one number is missing. If the difference is three, then two numbers are missing, etc. This works on a sorted array. Your algorithm works on both sorted and unsorted arrays, but it's more complicated than what I described for sorted arrays. \$\endgroup\$sum(1 to n) = Sum(array) + m1 + m2
andsumOfSquares(1 to n) = SumOfSquares(array) + m1^2 + m2 ^ 2
. Solve for m1 and m2. As the number of missing numbers increases, this approach becomes untenable. I'd recommend an in-place bucket sort. This runs in linear time (even though it's a sorting-algorithm) and requires only one pass through the array. \$\endgroup\$