Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete duplicates in an array

Status
Not open for further replies.

nikscrasher

Programmer
Aug 11, 2003
6
PH
Sir/Ma'am,
I have some question in javascript, I want to delete the values that is greater than 5 instances in an array.
For example here's my array
myarray = [123456,234567,741852,963852,
123456,123456,123456,123456,123456];

Thank you in advance...
 
Javascript from PHP:

/**
* array_unique -- Removes duplicate values from an array
*
* array_unique() takes input array and returns a new array without duplicate values.
* Note that keys are NOT preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.
* Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
* The first element will be used.
*
* @param Array array
* @return Array r
* @author Mick <mick@webpower.nl>
* @copyright LGPL
*/
function array_unique(array) {
var array = array;
var r_array = new Array();

for (var i = 0; i < array.length; i++) {
if (in_array(array, r_array))
continue;

r_array[(r_array.length)] = array;
}

return r_array;
}
function arrayUnique() {
return array_unique(this);
}
Array.prototype.array_unique = arrayUnique;

Now do:
myarray.array_unique();

Solved!

mcvdmvs
&quot;It never hurts to help&quot; -- Eek the Cat
 

mcvdmvs,

"in_array" is nowhere to be seen.

nikscrasher,

Do you want to delete all the occurrences of a number that appears more than 5 times, or only those occurrences after 5 times? Your post does not make this clear.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top