Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test2 {
- // input values:
- final static int[] data = {1, 2, 3, 4, 4, -1, 5, 5, 6, 7, 1};
- // array to hold unique values:
- static int[] unique = new int[data.length];
- // array elements in 'unique' from 0 to 'extent' are meaningful (rest are 0)
- static int extent = 0;
- public static void main(String args[]) {
- // put input values into result array
- for (int val : data) {
- add(val);
- }
- // show result
- for (int index = 0; index < extent; index++) {
- System.out.println(unique[index]);
- }
- }
- // add parameter 'value' into the array 'unique', if it is not
- // already present
- public static void add(int value) {
- // go through 'unique'
- for (int index = 0; index < extent; index++) {
- // if found..
- if (value == unique[index]) {
- return; // leave
- }
- }
- // else add it
- unique[extent] = value;
- extent++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment