Skip to main content
Added output on each step in example
Source Link
Imus
  • 4.3k
  • 9
  • 25

edit: An example since it wasn't clear yet: userInputs = {0,0,0,0,0} nextNumberIndex = 0

userInputs = {0,0,0,0,0}
nextNumberIndex = 0

The user inputs: 10 userInputs = {10, 0, 0, 0, 0} nextNumberIndex = 1

userInputs = {10, 0, 0, 0, 0}
nextNumberIndex = 1

output is 10

The user inputs: 12 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2

userInputs = {10, 12, 0, 0, 0}
nextNumberIndex = 2

output is 10, 12

The user inputs: 10 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2 notice

userInputs = {10, 12, 0, 0, 0}
nextNumberIndex = 2

output is 10, 12 again

notice how this input is completely ignored here, because it's a duplicate.

The user inputs: 99 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

userInputs = {10, 12, 99, 0, 0}
nextNumberIndex = 3

output is 10, 12, 99

The user inputs: 10 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

userInputs = {10, 12, 99, 0, 0}
nextNumberIndex = 3

output is 10, 12, 99 again

Then toTo print our result we use

This will result in the numbers 10, 12, 99 (at the end) and stop because the next i is equal to nextNumberIndex so the for loop ends. These are exactly the non-duplicat numbers that we wanted.

edit: An example since it wasn't clear yet: userInputs = {0,0,0,0,0} nextNumberIndex = 0

The user inputs: 10 userInputs = {10, 0, 0, 0, 0} nextNumberIndex = 1

The user inputs: 12 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2

The user inputs: 10 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2 notice how this input is completely ignored here, because it's a duplicate.

The user inputs: 99 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

The user inputs: 10 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

Then to print our result we use

This will result in the numbers 10, 12, 99 and stop because the next i is equal to nextNumberIndex so the for loop ends. These are exactly the non-duplicat numbers that we wanted.

edit: An example since it wasn't clear yet:

userInputs = {0,0,0,0,0}
nextNumberIndex = 0

The user inputs: 10

userInputs = {10, 0, 0, 0, 0}
nextNumberIndex = 1

output is 10

The user inputs: 12

userInputs = {10, 12, 0, 0, 0}
nextNumberIndex = 2

output is 10, 12

The user inputs: 10

userInputs = {10, 12, 0, 0, 0}
nextNumberIndex = 2

output is 10, 12 again

notice how this input is completely ignored here, because it's a duplicate.

The user inputs: 99

userInputs = {10, 12, 99, 0, 0}
nextNumberIndex = 3

output is 10, 12, 99

The user inputs: 10

userInputs = {10, 12, 99, 0, 0}
nextNumberIndex = 3

output is 10, 12, 99 again

To print our result we use

This will result in the numbers 10, 12, 99 (at the end) and stop because the next i is equal to nextNumberIndex so the for loop ends. These are exactly the non-duplicat numbers that we wanted.

Provided an example.
Source Link
Imus
  • 4.3k
  • 9
  • 25

edit: An example since it wasn't clear yet: userInputs = {0,0,0,0,0} nextNumberIndex = 0

The user inputs: 10 userInputs = {10, 0, 0, 0, 0} nextNumberIndex = 1

The user inputs: 12 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2

The user inputs: 10 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2 notice how this input is completely ignored here, because it's a duplicate.

The user inputs: 99 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

The user inputs: 10 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

Then to print our result we use

for(int i = 0; i < nextNumberIndex;i++){
    //handle userInputs[i]
}

This will result in the numbers 10, 12, 99 and stop because the next i is equal to nextNumberIndex so the for loop ends. These are exactly the non-duplicat numbers that we wanted.

Good luck with the next version of your solution!

Good luck with the next version of your solution!

edit: An example since it wasn't clear yet: userInputs = {0,0,0,0,0} nextNumberIndex = 0

The user inputs: 10 userInputs = {10, 0, 0, 0, 0} nextNumberIndex = 1

The user inputs: 12 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2

The user inputs: 10 userInputs = {10, 12, 0, 0, 0} nextNumberIndex = 2 notice how this input is completely ignored here, because it's a duplicate.

The user inputs: 99 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

The user inputs: 10 userInputs = {10, 12, 99, 0, 0} nextNumberIndex = 3

Then to print our result we use

for(int i = 0; i < nextNumberIndex;i++){
    //handle userInputs[i]
}

This will result in the numbers 10, 12, 99 and stop because the next i is equal to nextNumberIndex so the for loop ends. These are exactly the non-duplicat numbers that we wanted.

Good luck with the next version of your solution!

Removed the userInput initialisation to -1 because it doesn't matter at all. We only look at the array up to where it's filled with numbers from user input.
Source Link
Imus
  • 4.3k
  • 9
  • 25

But this requires some other changes as well. The easiest but kinda quick and dirty way is to assume that the user will only be allowed to enter positive numbers. This way we can initialise the userInputs array with all -1 numbers and where needed check if the number is negative to know if it was a user input or placeholder.

So in the constructor we add

Arrays.fill(userInputs, -1);

Or if you want to do it in the traditional way (because you didn't know that method existed):

for(int i = 0; i<userInputs.length();i++){
    userInputs[i] = -1;
}

Next to We first make it easy for ourselves we introduceby introducing a new variable:

This one keeps track of what index the next number should be inserted if it's unique. And to know which numbers in this array were input from the user.

But this requires some other changes as well. The easiest but kinda quick and dirty way is to assume that the user will only be allowed to enter positive numbers. This way we can initialise the userInputs array with all -1 numbers and where needed check if the number is negative to know if it was a user input or placeholder.

So in the constructor we add

Arrays.fill(userInputs, -1);

Or if you want to do it in the traditional way (because you didn't know that method existed):

for(int i = 0; i<userInputs.length();i++){
    userInputs[i] = -1;
}

Next to make it easy for ourselves we introduce a new variable:

This one keeps track of what index the next number should be inserted if it's unique.

But this requires some other changes as well. We first make it easy for ourselves by introducing a new variable:

This one keeps track of what index the next number should be inserted if it's unique. And to know which numbers in this array were input from the user.

Source Link
Imus
  • 4.3k
  • 9
  • 25
Loading