?
Solved

given a sorted integer array, return a new integer array without duplicates

Posted on 2004-11-24
3
Medium Priority
?
364 Views
Last Modified: 2010-04-17
I have to write a function that given a sorted integer array would return a new integer array without duplicates.
What is the best solution? Is there better solution than looping through the whole array and checking if the value has changed?
0
Comment
Question by:quasar_ee
3 Comments
 
LVL 3

Accepted Solution

by:
HendrikTYR earned 500 total points
ID: 12669268
If your array is already sorted, then the best is indeed to loop through it, adding the value to your new array if it differs from the last value.
0
 
LVL 7

Assisted Solution

by:suramsureshbabu
suramsureshbabu earned 360 total points
ID: 12669306
looping is the only way of doing it.

first read the values in array1
start creating the new array
check for the values if they are existing in the new array before adding it
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 140 total points
ID: 12669571
                      $prev = "not equal to $in[0]";
                       @out = grep($_ ne $prev && ($prev = $_, 1), @in);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Six Sigma Control Plans
Starting up a Project

862 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