Solved

java again

Posted on 2003-12-10
9
344 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:sandipmurmu
9 Comments
 
LVL 2

Expert Comment

by:DidierD
ID: 9918611
First start with grading your 9 open questions.

Greetz,
Didier
0
 

Author Comment

by:sandipmurmu
ID: 9919352
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
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9919691
Sort the array first.  This way all the identical words will be next to each other.

Arrays.sort(ar);
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Accepted Solution

by:
Mayank S earned 20 total points
ID: 9919706
There are plenty of ways of doing that. I am not giving you any sample code, but I'll tell you how to do it. You can try it and if you have any problems, then get back to us.

1. Maintain two arrays.
2. Store all the data in the first array.
3. Start reading from the first array.
4. For every element in the first array, check if it is there in the second array. If yes, then do't add it, otherwise add it to the second array.
5. The second array will now have no duplicates.

Mayank.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9919731
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/classwork 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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9920133
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.
0
 

Expert Comment

by:Programmer_to_be
ID: 9922666
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.
0
 
LVL 1

Expert Comment

by:tutyfruity
ID: 10008099
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,
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10009281
tutyfruity,

That is the selection sort loop. Not bubble sort.

Mayank.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question