Finding duplicate in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Finding duplicate in an array

Post by kkonline »

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
Last edited by kkonline on Wed Oct 24, 2007 10:00 am, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

To remove the duplicates you can use the built in function, array_unique.
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

Zoxive wrote: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 info
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

How are you doing it at the moment?
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

onion2k wrote:How are you doing it at the moment?
Note :the values are not sorted and

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
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Sorry, does this relate to a previous thread?

You stated your aim ...
The aim: to display/remove the duplicate entries in array
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 ...

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);
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.
Post Reply