Solved

java again

Posted on 2003-12-10
9
341 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
 
LVL 30

Accepted Solution

by:
mayankeagle 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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:mayankeagle
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:mayankeagle
ID: 10009281
tutyfruity,

That is the selection sort loop. Not bubble sort.

Mayank.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now