sandipmurmu
asked on
java again
suppose we have an array of words,
of which there are some similar words in the array.
how can we determine and extract the words that are repeated in the array.
eg., ar[]={me, she, ours, me, their}
we need to code such that
ar1[]={me, she, ours, their}
indicating 'me':2(i.e., preset twice)
bye,
sandip
of which there are some similar words in the array.
how can we determine and extract the words that are repeated in the array.
eg., ar[]={me, she, ours, me, their}
we need to code such that
ar1[]={me, she, ours, their}
indicating 'me':2(i.e., preset twice)
bye,
sandip
ASKER
suppose we have an array of words,
of which there are some similar words in the array.
how can we determine and extract the words that are repeated in the array.
eg., ar[]={me, she, ours, me, their}
we need to code such that
ar1[]={me, she, ours, their}
indicating 'me':2(i.e., preset twice)
bye,
sandip
of which there are some similar words in the array.
how can we determine and extract the words that are repeated in the array.
eg., ar[]={me, she, ours, me, their}
we need to code such that
ar1[]={me, she, ours, their}
indicating 'me':2(i.e., preset twice)
bye,
sandip
Sort the array first. This way all the identical words will be next to each other.
Arrays.sort(ar);
Arrays.sort(ar);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good alternative mayankeagle ;-)
I'm always concerned with the problem of array sizes. In this case, for example, there is no way of knowing how big the second array should be.
It depends on what the project/assignment/classwo rk restrictions are, but this is where the collections classes come in useful.
Using mayankeagles principle, you could create an ArrayList to contain the "distinct" strings and this also simplifies checking to see whether a string is already present or not. With an array solution, you'll need to check each string in the new array manually, with a collection (like an ArrayList), you can just use the contains() method.
I'm always concerned with the problem of array sizes. In this case, for example, there is no way of knowing how big the second array should be.
It depends on what the project/assignment/classwo
Using mayankeagles principle, you could create an ArrayList to contain the "distinct" strings and this also simplifies checking to see whether a string is already present or not. With an array solution, you'll need to check each string in the new array manually, with a collection (like an ArrayList), you can just use the contains() method.
Well, yeah, it would be better to do that. Since its Java, you need to know the size of the array before allocating memory dynamically for it. You can use an ArrayList instead.
Hi sandipmurmu,
I had challenged myself with a problem like that once, my solution was very similar to the one mayankeagle posted, worked great.
MR S.M.
I had challenged myself with a problem like that once, my solution was very similar to the one mayankeagle posted, worked great.
MR S.M.
use the bubble sort nested loops
for (int i=0;i<n-1;i++)
for (int j=i+1;j<n;j++)
compare the content of the inedxes and shift in case of equality
use the same array,
for (int i=0;i<n-1;i++)
for (int j=i+1;j<n;j++)
compare the content of the inedxes and shift in case of equality
use the same array,
tutyfruity,
That is the selection sort loop. Not bubble sort.
Mayank.
That is the selection sort loop. Not bubble sort.
Mayank.
Greetz,
Didier