Solved

java again

Posted on 2003-12-10
9
343 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

15 Experts available now in Live!

Get 1:1 Help Now