Datasets:

ArXiv:
License:
File size: 1,879 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class NoDupArray {

    private long[] a;//ref to array a
    private int nEliments;//number of data items

    public NoDupArray(int max) {//constructor
        a = new long[max];
        nEliments = 0;
    }

    public boolean find(long SearchKey) {//find specified value
        for (int i = 0; i < nEliments; i++) {
            if (a[i] == SearchKey) {
                return true;
            }
        }
        return false;
    }// end find()

    public void insert(long value) {//put element into array

        if (this.nEliments == this.a.length) {
            System.out.println("array is full");
        } else {
            if (find(value)) {
                System.out.println("value allready exist");
            } else {
                a[this.nEliments] = value;
                this.nEliments++;
            }
        }
    }//end insert()
    public boolean delete(long value) {//delete the element if it found
        for (int i = 0; i < this.nEliments; i++) {
            if(this.a[i]== value){
                for(int j=i;j<this.nEliments-1;j++){
                    this.a[j]=this.a[j+1];
                }
                this.nEliments--;
                System.out.println("value deleted");
                return true;
            }
        }
        System.out.println("value not found;");
        return false;
    }//end delete()

    public void display() {//display array contents
        System.out.println("values in the array-----------");
        for(int i=0;i<this.nEliments;i++){
            System.out.print(this.a[i]+" ");
        }
        System.out.println();
    } //end display()
}

public class NoDupArrayApp {
    public static void main(String[] args) {
        NoDupArray x= new NoDupArray(6);
        x.insert(9);
        x.insert(5);
        x.insert(5);
        x.insert(6);
        x.insert(2);
        x.display();
  }

}