Solved

java again

Posted on 2003-12-10
9
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

622 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