Java Exercise: Problems in Searching

Array is a fundamental data structure in Java programming language. It stores a fixed number of items, and we can use index to access each element.

The following 7 exercises are about searching and sorting an array.

Screen Shot 2018-04-17 at 20.26.36
Exercise 1

To find the occurrence of a number, I used a for loop to go through the array. Using the parameter key, I can use a if statement to check if the desired element is found and increment the integer variable “occurrence.”

Screen Shot 2018-04-17 at 20.27.17
Exercise 2

As the description suggests, we need to find the duplicate elements. Given an unsorted array, I used a inner loop, which starts from the index+1 of the outer loop. In the inner loop, is duplicate element is found once, the program will print the duplicate elements. This prevent the program to print the same duplicate element for more than one times (e.g. if 3 appears 2 times, the program won’t print two “3”s).

Screen Shot 2018-04-17 at 20.27.28
Exercise 3

To find the element which appears maximum times, I used an inner loop again to record the occurrence of each number. By comparing the temporary occurrence to the former maximum value, I can record that index and return it. Note that any time a number with the same max occurrence is found, the index will be overwritten.

Screen Shot 2018-04-17 at 20.27.38
Exercise 4

Since one element is missing in the sorted array, I just calculated the desired sum by incrementing a variable inside the loop. Then, I subtract the sum of the array elements from the desired sum (with the missing one), and the missing element is shown.

Screen Shot 2018-04-17 at 20.27.48
Exercise 5

For this exercise, I still used two loops with the second one starting from the index+1 of the first loop. In the inner loop, I used an if conditional statement: if the sum equals the desired value, the two elements are returned. I also found out that the flag “found” is useless because I print every pairs each time I checked them. Also, since the inner loop begins at i+1, there will be no duplicated pairs.

Screen Shot 2018-04-17 at 20.28.00
Exercise 6

To separate the even and odd numbers, I create a new array “separated,” which has the same length as the argument. The mechanism is that each time I find an even number, I put it from the beginning, and each time I find an odd number, I put it from the last spot. I use a and b to be the current available spot for even and odd number respectively. Also, I increment i to loop through the argument array. Since there are three indexes, I used a while loop to simplify the loop. For example, when a new odd number is inserted, I will count down the value of b.

Screen Shot 2018-04-17 at 20.28.19
Exercise 7

This question asks me to find the max and min value and return both the index and value. So I will first create the temporary max and min value, then loop through the array to compare the current max and min with the current value, then find out the actual max and min index. Then, I print them using the required format.

Screen Shot 2018-04-17 at 20.29.10
Output from the Testing Program

Here is a testing program I wrote in a main method. It shows that everything works fine. To conclude, when doing this kind of programming question, we need to know how many loops we are going to use and then identify how we will increment the index. We may also consider whether we should use flags or additional temporary arrays.

 

Website Built with WordPress.com.

Up ↑