Pages

Thursday, 25 August 2016

Chapter 7 Exercise 15, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

7.15 (Eliminate duplicates) Write a method that returns a new array by eliminating the duplicate values in the array using the following method header:
public static int[] eliminateDuplicates(int[] list)
Write a test program that reads in ten integers, invokes the method, and displays the result. Here is the sample run of the program:
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5 

import java.util.Scanner;
 
 
public class ProgrammingEx7_15 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  int[] numbers = new int[10];
 
  System.out.print("Enter ten numbers:");
 
  for (int i = 0; i < numbers.length; i++) {
   numbers[i] = input.nextInt();
  }
 
  System.out.print("The distinct numbers are:");
 
  numbers = eliminateDuplicates(numbers);
  for (int i = 0; i < numbers.length; i++) {
   System.out.print(numbers[i] + " ");
  }
 }
 
 public static int[] eliminateDuplicates(int[] list) {
 
  int[] newArray = new int[0];
  boolean flag = true;
 
  newArray = add2Array(newArray, list[0]);
  for (int i = 0; i < list.length; i++) {
   for (int j = 0; j < newArray.length; j++) {
 
    if (list[i] == newArray[j]) {
     flag = false;
     break;
    }
 
   }
 
   if (flag) {
    newArray = add2Array(newArray, list[i]);
   }
    
   flag= true;
 
  }
 
  return newArray;
 
 }
 
 // Copy array numbers1 to array numbers2
 // If the length of array numbers2 is less then array numbers2, return false
 public static boolean copyArray(int[] source, int[] dest) {
  if (source.length > dest.length)
   return false;
 
  for (int i = 0; i < source.length; i++) {
   dest[i] = source[i];
  }
  return true;
 }
 
 // Increase array size by one and add integer to the end of array
 // Return new array
 public static int[] add2Array(int[] source, int data) {
 
  int[] dest = new int[source.length + 1];
  copyArray(source, dest);
  dest[source.length] = data;
  return dest;
 
 }
 
}

No comments :

Post a Comment