waltermilner

Untitled

Jul 26th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.03 KB | None | 0 0
  1. public class Test2 {
  2.     // input values:
  3.     final static int[] data = {1, 2, 3, 4, 4, -1, 5, 5, 6, 7, 1};
  4.     // array to hold unique values:
  5.     static int[] unique = new int[data.length];
  6.     // array elements in 'unique' from 0 to 'extent' are meaningful (rest are 0)
  7.     static int extent = 0;
  8.  
  9.     public static void main(String args[]) {
  10.         // put input values into result array
  11.         for (int val : data) {
  12.             add(val);
  13.         }
  14.         // show result
  15.         for (int index = 0; index < extent; index++) {
  16.             System.out.println(unique[index]);
  17.         }
  18.  
  19.     }
  20.  
  21.     // add parameter 'value' into the array 'unique', if it is not
  22.     // already present
  23.     public static void add(int value) {
  24.         // go through 'unique'
  25.         for (int index = 0; index < extent; index++) {
  26.             // if found..
  27.             if (value == unique[index]) {
  28.                 return; // leave
  29.             }
  30.         }
  31.         // else add it
  32.         unique[extent] = value;
  33.         extent++;
  34.  
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment