I have an array with some integers.
i want to remove redudants integers in the array and print the integers without redudancy...
can any one give me the code please.....
I have an array with some integers.
i want to remove redudants integers in the array and print the integers without redudancy...
can any one give me the code please.....
I have an array with some integers.
i want to remove redudants integers in the array and print the integers without redudancy...can any one give me the code please.....
Your answer is here.. last point in this thread
http://www.daniweb.com/forums/thread78060.html
Mr.sree_ec
for ur kind information....i already have the code for this question.
I just came across C threads and felt like posting this question coz this is an important one for many interviews.... I thought it might be useful for some people.....
Your answer is here.. last point in this thread
http://www.daniweb.com/forums/thread78060.html
Don't post something just becaue you might think it might be useful to someone some day. I was going to answer the question, but now I'm glad I didn't wast my time on it.
Mr.sree_ec
for ur kind information....i already have the code for this question.
I just came across C threads and felt like posting this question coz this is an important one for many interviews.... I thought it might be useful for some people.....
This is not a forum to do social service and you cannot waste other's time asking questions like this when you already know the answer.
<END>
int unique(int *array, int number) //sorted array
{
int k = 0;
for (int i = 1; i < n; i++) {
if (a[k] != a[i]) { //comparing the first 2 unequal numbers
a[k+1] = a[i]; //shifting to the left if distinct
k++;
}
}
return (k+1); //return the last index of new array i.e. the number of distinct elements
}
The code you posted doesn't compile. You should at least compile and test it before attempting to post it as an example of how to do something.
Sorry, but that will not work for an array that contains random numbers, where duplicates can appear in any order throughout the array.
Test your code with this array
1 5 6 1 2 5 4 3 1 7 6
The code you posted doesn't compile. You should at least compile and test it before attempting to post it as an example of how to do something.
Sorry, but that will not work for an array that contains random numbers, where duplicates can appear in any order throughout the array.
Test your code with this array
1 5 6 1 2 5 4 3 1 7 6
The array is sorted first. See the comment in the code.
O,I'll give you that one, but it still contains syntax errors.
Sorry the above code act as pseudo code. If u want exact code then here it is:-
int unique(int *array, int number) //sorted array
{
int k = 0;
for (int i = 1; i < number; i++) {
if (array[k] != array[i]) { //comparing the first 2 unequal numbers
array[k+1] = array[i]; //shifting to the left if distinct
k++;
}
}
return (k+1); //return the last index of new array i.e. the number of distinct elements
}
Just call the above function in main using correct parameters and it will work fine.
You are burying yourself a little deeper -- that was not pseudo code.
Just talk it straight. What are u trying to say? I thought this is a technical forum bt reality seems something else. What u want to prove. A have given a simple function and its working fine on my compiler(Dev c++ 4.9.9.2). If its giving any error on your compiler thats your problem. This is a humble request "Dont waste your and mine time".
Algoritmus, your code is OK.
Understand that Ancient Dragon is being technically correct and please don't:
1) Get upset. Get comfortable in your skin, and "thicken" it. Don't feel threatened by a critique of your code or pseudo code. That's what we do here!
2) Please stop making excuses. "It's a simple function", "it's pseudo code". Neither invalidates the critique made.
No one is at the top of their form all the time, no one is right 100% of the time. If the creator of C can make errors in his book on C, you and I, and AD, can (and will), make errors as well.
It doesn't mean you're not a good person, or a good programmer. Just PLEASE don't make the mistake that sends the guys away from Mars, heading for Jupiter instead. That's all I ask. << C'mon smile >> ;) ;)
I apologize for my behavior. I ensure this will not repeat in future.
:D :D
Lots of weird things going on in this thread, but I don't care if its a trap or not, I saw horrible code and like a magnet I had to fix it!
So maybe there is some use to this function for someone else.
I made this code literally in 10 minutes, tested it on GCC. Works like a charm.
#define QS_MAX_LEVELS 300
int Unique_QuickSortMethod(int *arr, int elements) {
// Sort the list using the Quick Sort algorithm
int piv, beg[QS_MAX_LEVELS], end[QS_MAX_LEVELS], i=0, L, R, swap ;
beg[0]=0; end[0]=elements;
while (i>=0) {
L=beg[i]; R=end[i]-1;
if (L<R) {
piv=arr[L];
while (L<R) {
while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R];
while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L];
}
arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L;
if (end[i]-beg[i]>end[i-1]-beg[i-1]) {
swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap;
swap=end[i]; end[i]=end[i-1]; end[i-1]=swap;
}
} else i--;
}
// Shift out all the duplicates.
// NOTE: This code can be optimized significantly by
// writing the non-duplicates to a new array rather
// than shifting. I am using the shifting technique
// since it was used in an earlier post, but it is
// highly inefficient.
int outer=1, inner=1, elements_return = elements;
for (;outer<elements_return;outer++)
if (arr[outer]==arr[outer-1]) {
for (inner=outer+1;inner<elements_return;inner++)
arr[inner-1]=arr[inner];
elements_return--;
outer--;
}
return elements_return;
}
If you want me to write the non-shifting technique which is far superior to the shift technique employed here, let me know, but this is just to fix bad code.
I don't agree with giving away answers, but I strongly believe that clearly written code can offer insight to those willing to learn from it.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.