if input is 10,20,30,10,40,20,30 output should be 10,20,30 ,0,40,0,0 all repeated element should be denoted by 0 and shifted to end of the list
Code in c++ to arrang the list in following manner?
//I'm assuming that dim is the number of elements in the list
//so the list items go from list[0] to list[dim-1]
int i=0;
while i%26lt;dim
{
int j = 0;
int found = 0;
while (j%26lt;i %26amp;%26amp; found==0)
{
if list[i] == list[j] {found = 1}
j++;
}
if (found==1) list[i] = 0;
i++;
}
Explanation:
i represents the current element in the list
For every i, the second loop (with j) checks to see if any of the elements is equal to it. As soon as it found the first equal element, or after having checked all elements with a lower index (within the list) as the current element, it stops.
If it does find an element that is equal to list[i], then the variable found gets the value 1.
After exiting the second loop, I check if another occurence has been found. If yes, I change the value of list[i] to 0.
P.S. I'm assuming that list is an int[dim].
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment