In: Computer Science
Design (pseudocode) and implement (source code) a program (name it DistinctValues) to display only district values in an array. The program main method defines a single-dimensional array of size 10 elements and prompts the user to enter 10 integers to initialize the array. The main method then calls method getValues() that takes an integer array and returns another single-dimensional array containing only distinct values in the original (passed) array. Document your code and properly label the input prompts and the outputs as shown below.
Sample run 1:
Original array: 5 2 1 5 2 3 2 1 5 9
Distinct array: 5 2 1 3 9
import java.util.Scanner; public class DistinctValues { public static int[] getValues(int[] arr) { int count = 0; for(int i = 0; i <arr.length; ++i) { boolean found = false; for(int j = 0; j < i; ++j) { if(arr[i] == arr[j]) { found = true; } } if(!found) { ++count; } } int[] result = new int[count]; int ind = 0; for(int i = 0; i <arr.length; ++i) { boolean found = false; for(int j = 0; j < i; ++j) { if(arr[i] == arr[j]) { found = true; } } if(!found) { result[ind++] = arr[i]; } } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Original array: "); int[] arr = new int[10]; for(int i = 0; i < arr.length; ++i) { arr[i] = in.nextInt(); } int[] result = getValues(arr); System.out.print("Distinct array: "); for(int i = 0; i < result.length; ++i) { System.out.print(result[i]); if(i != result.length-1){ System.out.print(" "); } } System.out.println(); } }