Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java again

Posted on 2003-12-10
9
Medium Priority
?
350 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 30

Accepted Solution

by:
Mayank S earned 80 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
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 …

688 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