I have an array of positive integers.
The length of array is known "n".
The maximum and the minimum values in the arrays are known (thay are unique for simplicity)
The aim: to display/remove the duplicate entries in array
I cannot make out what to do with the minimum and maximum values to make the algorithm efficient
Finding duplicate in an array
Moderator: General Moderators
Finding duplicate in an array
Last edited by kkonline on Wed Oct 24, 2007 10:00 am, edited 1 time in total.
To remove the duplicates you can use the built in function, array_unique.
Hi, thanks for that but i am looking for an algorithm using all the given infoZoxive wrote:To remove the duplicates you can use the built in function, array_unique.
Note :the values are not sorted andonion2k wrote:How are you doing it at the moment?
Just taking the 1st value comparing with all array elements, if found in array then duplicate else not...
but in this case max. and min. values are not used what to do
or another algo
subtract each value from max and then store the difference if difference same then duplicate.
in this min. value is not used
Sorry, does this relate to a previous thread?
You stated your aim ...
I haven't tested it, but $arr_new should hold all values with just duplicates removed, and $arr_dupes should hold all of the discarded duplicate items, so you can display it or whatever.
This should take care of the items you specify as your aim. If there is some other requirement, perhaps you need to give more info, or an updated aim.
Cheers
EDIT | You replied while I was posting, so sorry if my answer seems a bit disjointed. Any reason why you dont just sort the array before looping. You could just call it a dupe if the current one matches the last.
You stated your aim ...
And I'm pretty sure that array_unique() does just that. The removal part anyhow. As for the display, I guess you could find the duplicates for whatever purpose by, say ...The aim: to display/remove the duplicate entries in array
Code: Select all
$arr_old = array('cat', 'dog', 'monkey', 'dog');
$arr_new = array_unique($arr_old);
$arr_dupes = array_diff_assoc($arr_old, $arr_new);
This should take care of the items you specify as your aim. If there is some other requirement, perhaps you need to give more info, or an updated aim.
Cheers
EDIT | You replied while I was posting, so sorry if my answer seems a bit disjointed. Any reason why you dont just sort the array before looping. You could just call it a dupe if the current one matches the last.