...

View Full Version : Finding duplicate numbers in an array?



TheDon
08-22-2002, 09:41 AM
hi everyone, im new here so i hope i this question hasnt been asked before. i did a search but saw that it hadnt.

i relatively new to javascript, and the only other programming i have done is VB so the syntax is quite strange and so is some of the logic.

what im trying to do is

Using arrays I input 10 numbers. As each number is read, display it only if it is not a duplicate of a number that has already been read.

this is what i have done so far, but it doesnt work and im not even sure if im on the right path -

var a = new Array( 5 );
var b = new Array( 5);

for( var i = 0; i < a.length; i++ )
a[ i ] = window.prompt ("Enter a number.","");
b[ i ] == a[ i ];
{
if (a[ i ] != 0)
{
for ( i = 0; i < a.length; i++ )
if ( a[ i ] != b[ i ] )
document.writeln( a[ i ] );
}
}

Thanks for your help.

TheDon
08-22-2002, 10:50 AM
I decided to go about it in another way and try something different
my code

var a = new Array( 5 );

for ( var i = 0; i < a.length; ++i )
var input = window.prompt("Enter number","");
a[ i ] = input;

var element = Search( a, input );

if(element != -1 )
document.writeln( i );
else
document.writeln( "not duplicate" );

function Search( theArray, key )
{
for( var n = 0; n < theArray.length; ++n )
if( theArray[ n ] == key )
return n;

return -1;
}



Is this anywhere near closer or am i still off the track?

joh6nn
08-22-2002, 10:53 AM
i came up with the following, which does what you want:

Array.prototype.indexOf = function(value, start) {
var index = -1;
for ( var i = start || 0; i < this.length; i++) { if (this[i] == value) { index = i; break; } }
return index;
}

which returns either the slot in the array where the value was found, or -1 if the value can't be found. however, that's not quite complete, and jkd modified it to this, which is a bit more thorough:

Object.prototype.hashdexOf = function(value) {
var response = -1;
for (var key in this) {
if (this[key] == value) {
response = /\D/.test(key) ? key : Number(key);
break;
}
}
return response;
}

you use it like this:

if ( arrayName.hashdexOf(5) == -1 ) {
alert("Value not found");
}
else {
alert("Value found at " + arrayName.hashdexOf(5) );
}

TheDon
08-22-2002, 11:06 AM
ummm.....thanks heaps

i kinda dont understand it at all though

where do i fit this code in though? does it go in with my code or is it a complete new thing? where does the input go?

what is var key in this ?

what is Object.prototype.hashdexOf and /\D/., ?
in var i = start || 0 what is the ||?

sorry if im asking too many questions
thanks

joh6nn
08-22-2002, 10:29 PM
sorry, kinda got a bit ahead of myself.

when you have an object ( for example, an Array ), you can attach functions to it. these functions are supposed to ( but don't have to ) work specifically on the information that makes up the object ( in this case, the information in the Array ). these kinds of functions are called methods. if you have a method that you want to apply to every object of this type ( you want to be able to use this method on every Array, and not just this Array ), then you can use the prototype property, which gives you access to the constructor used to create all objects of that type ( all the Arrays in the script ). so, that's the Array.prototype part.

Strings have a method called indexOf(). it lets you search for a certain value inside of that string. if it finds the value, then it returns the position of that value in the string. if it can't find that value, it returns -1. for example:

var string1 = "abcdef";
var string2 = "cd";
alert(string1.indexOf(string2)); // alerts 2, because c is at positiong 2 in the string.
alert(string1.indexOf("z"); // alerts -1, because z is not in the string.

the method that i wrote for Arrays mimics the functionality of the String's indexOf method. however, i forgot about Hashes.

A Hash is an associative Array ( in JavaScript, any Object can be treated like a Hash ). In a normal Array, you reference things by number. In a Hash, you reference things by name. for instance:

//normal Array.
arrayName[1];

//Hash ( any Object in javaScript )
hashName["something"];

my method doesn't work for Hashes. jkd, one of the moderators, took my idea, and modified it so that it works with Hashes. This means, you can check to see if a value is in any kind of Object, and not just in an Array.

there are two reasons to name this new method for Objects "hashdexOf" . first, so that you know that it works on Objects ( Hashes ), and secondly, because Strings already have an "indexOf" method, and if you name this new method "indexOf", it will overwrite that method for Strings.

TheDon
08-23-2002, 02:10 AM
Ok so do i put the code given above in with my code or is it completely different?
Also where do i put the input?

joh6nn
08-23-2002, 02:37 AM
this code is completely separate from yours. so that you know, though, your second try wasn't very far off.

here's an example of using the hashdexOf() method:

<script>
Object.prototype.hashdexOf = function(value) {
var response = -1;
for (var key in this) {
if (this[key] == value) {
response = /\D/.test(key) ? key : Number(key);
break;
}
}
return response;
}

var array1 = [1,2,3,4,5,"jimmy"];
var input = window.prompt("Enter a number");
if ( array1.hashdexOf(input) != -1 ) {
alert("value found!");
}
</script>

TheDon
08-23-2002, 08:54 AM
i can see how your code works, but i still dont understand
response = /\D/.test(key) ? key : Number(key);
line of code.

preferably i want to do it with a for loop, so that it automatically searches through for duplicate numbers, rather than entering which ones you want to find manually.

sorry, its just some of the stuff in the code is quite confusing for me, in terms of the syntax

joeframbach
02-10-2003, 02:32 PM
this could make for a good Code Challenge!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum