Removing all duplicated numbers

Hello,

could you help me fix my RemoveDuplicates method, its sopose to remove all numbers that were duplicated and not leave one.


this is my incorrect out put.


Initial List: 
12 4 25 6 4 -2 8 100 4 810 
Item to Delete: 4
List after removing all duplicates: 
12 810 25 6 4 -2 8 100


this is what has to show up.


Initial List: 
12 4 25 6 4 -2 8 100 4 810 
Item to Delete: 4
List after removing all duplicates: 
12 810 25 6 100 -2 8




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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>

using namespace std;

typedef int ItemType;

const int MAX_LENGTH = 100;

class List
{
        public:
                List();
                void Insert(ItemType item);
                void Delete(ItemType item);
                void ResetList();
                ItemType GetNextItem();
                int GetLength() const;
                bool IsEmpty() const;
                bool IsFull() const;
                bool IsThere(ItemType item) const;
                bool HasNext() const;
                void PrintForward() const;
                void PrintBackWard() const;
                void RemoveDuplicates(ItemType);
                int GetLocation(ItemType);
        private:
                int length;
                int currentPos;
                ItemType data[MAX_LENGTH];
};

int main()
{
        List L1;
        int n, x;
        cin>>n;
        for (int i = 0; i < n; i++)
        {
                cin>>x;
                L1.Insert(x);
        }

        int m;
        cin >> m;

        cout<<"Initial List: "<<endl;
        L1.PrintForward();
        cout<<"Item to Delete: "<< m << endl;
        cout<<"List after removing all duplicates: "<<endl;
        L1.RemoveDuplicates(m);
        L1.PrintForward();

        return 0;

}

List::List()
{
        length = 0;
        currentPos = 0;
}

bool List::IsEmpty() const
{
        return (length == 0);
}

bool List::IsFull() const
{
        return (length == MAX_LENGTH);
}

int List::GetLength() const
{
        return length;
}

void List::Insert(ItemType item)
{
        data[length] = item;
        length++;
}

void List::Delete(ItemType item)
{
        int index = 0;
        while (index < length && item != data[index])
                index++;
        if (index < length)
        {
                data[index] = data[length - 1];
                length--;
        }
}

bool List::IsThere(ItemType item) const
{
        int index = length - 1;
        while (index >= 0 && item != data[index])
                index--;
                return (index >= 0);
}

void List::ResetList()
{
        currentPos = 0;
}

bool List::HasNext() const
{
        return (currentPos != length);
}

ItemType List::GetNextItem()
{
        ItemType item;
        item = data[currentPos];
        currentPos++;
        return item;
}

void List::PrintForward() const  

{
        int i = 0;
        if (IsEmpty())
        {
                cout<<"List is Empty"<<endl;
                return;
        }
        while (i<length)
        {
                cout<<data[i]<<" ";
                i++;
        }
        cout<<endl;
}

void List::PrintBackWard()const
{
        int i = length;
        if (IsEmpty())
        {
                cout<<"List is Empty"<<endl;
                return;
        }
        while (i>0)
        {
                cout<<data[i-1]<<" ";
                i--;
        }
        cout<<endl;
}

int List::GetLocation(ItemType item)
{
        int index = 0;
        while (item != data[index])
                index++;
        return index;
}

void List::RemoveDuplicates(ItemType item)
{



        
        int index = 0;
        while (index < length && item != data[index])
        {
                index++;
                
                if (index < length && item == data[index] )
                {
                        data[index] = data[length - 1];
                        length--;
                
                }
                
        }

        
}
Last edited on
@line 90,91 you take the last item from the list and use it to overwrite the item you are deleting, this changes the order of the items in the list.

My suggestion is to modify List::DeleteItem() to take an index (so maybe rename to DeleteAt(int index))
In this routine shuffle all items up the list to close the gap where the deleted item was.
1
2
3
4
5
int lastIndex = length - 1;
while (index < lastIndex) {
    data[index] = data[index+1];
}
--length;


also at line 157, index needs to default to -1 so that you return an invalid index if the item is not found.

then List::RemoveDuplicates() can look like this...
1
2
3
4
5
6
7
8
do {
    int i = GetLocation(item);

    if (i != -1) {
        DeleteAt(i);
    }
} while (i != -1);
  


Last edited on
Topic archived. No new replies allowed.