Skip to main content

how can i find all duplicate values within an array?

FAQTs - Knowledge Base - View Entry - how can i find all duplicate values within an array?

how can i find all duplicate values within an array?
Sep 10th, 2004 13:29

Gavin Kistner, Jean-Bernard Valentaten, Periklis a

Well, there is more than one way to achieve this.
The easiest one is to walk the array position by position with two

for (var i=0; i{
for (var j=i+1; j {
if (myArray[i] == myArray[j])
myArray[j] = '';

This will do the job, but is very inefficient (especially with large
arrays), since a lot of comparisons are made. One might start to
optimize this code, but believe me, a slow algorithm will always stay
slow, no matter how much you optimize it.
The better way is to sort the array and then have a loop walk it once:


for (var i=0; i{
if (myArray[i] == myArray[i+1])
myArray[i+1] = '';

Note that the above solution does not account for objects stored in
the array which may be technically different (== computes to false)
but trivially the same. For example:

var myArray = [
{ name:'Gavin', age:31 },
{ name:'Lisa', age:30 },
{ name:'Gavin', age:23 },
{ name:'Gavin', age:31 }

In the above, technically
myArray[0] != myArray[3]
even though you know it is. For something like this you can write your
own comparison function:

Array.prototype.removeDuplicates = function( customCompare ){
if ( customCompare ){
for (var i=0; i<(this.length-1); i++)
if ( !customCompare(this[i],this[i+1]) ){
this.splice( (i--)+1, 1 );
for (var i=0; i<(this.length-1); i++)
if (this[i]==this[i+1]){
this.splice( (i--)+1, 1 );
return this;

You would call the above passing in the same sort of custom comparison
function as the myArray.sort() method takes:

myArray.removeDuplicates( function(a,b){
return ? 1 :
a.age b.age ? 1 : 0;
} );
Post a Comment

Popular posts from this blog

Compact and Repair an Access Database. Add Ref. to : AdoDb, Jro

< ?xml version="1.0" encoding="utf-8" ?>

using ADODB;
using JRO;
using System.Configuration;
using System.Data.OleDb;
using System.IO;

public class CompactAndRepairAccessDb : System.Windows.Forms.Form
private System.ComponentModel.Container components = null;
private JRO.JetEngine jro;
private System.Windows.Forms.Button btnConfirm;
private System.Windows.Forms.TextBox tbxOriginalDbSize;
private System.Windows.Forms.TextBox tbxCompactedDbSize;
private OleDbConnection cnn;

public CompactAndRepairAccessDb() {

FileInfo fi = new FileInfo( ConfigurationSettings.AppSettings["PathOriginal"] );
int s = Convert.ToInt32( fi.Length/1000 );
this.tbxOriginalDbSize.Text = s.ToString() + " kb";

private void btnConfirm_Click(object sender, System.EventArgs e) {
// First close all instances of the database

Creating ISO images with Nero 5.5 Express

Mark Michaelis' Weblog - August, 2003: "Creating ISO images with Nero 5.5 Express

I recently set up an old computer for my son, Benjamin, as he keeps messing up my wifes desktop and then I have to figure out how to get it back to the way she wants it. Anyway, as part of doing this I didn't want my son putting CDs in and out of the computer as he tends to scratch them. Instead, I decided to create ISO images of them and have him use them directly from the computer using Daemon-Tools. The problem, was how to create ISO images? I had a copy of Nero Express 5.5 but it took me some time to figure out exactly how to get it to make ISO images. (If you happen to have the full version of Nero you can find instructions for creating ISO images here.)

Here are the steps for Nero Express 5.5:

1. Launch Nero Express 5.5 (yes there is a 6 version out there but I don't have it.)
2. Select the Copy Entire Disk option.
3. Click the More>> button.
4. Click the Configure…