473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

getting arraylist elements with same value (not trivial. you have to read!)

i have a arraylist. say it contains integer values. i want to be able to
inform user, which indexes in the array contain same values. but there can
be N different values, and M different indexes having the same value.
actually i will do this with javascript array to inform user which input's
have same value. but i think its not a matter.
Nov 17 '05 #1
11 1930
>i have a arraylist. say it contains integer values. i want to be able to
inform user, which indexes in the array contain same values. but there can
be N different values, and M different indexes having the same value.
actually i will do this with javascript array to inform user which input's
have same value. but i think its not a matter.


what exactly is your problem? to find all duplicates at all, to write
effective code or to inform the user of duplicates?

Wiktor Zychla
Nov 17 '05 #2
three of them. find all duplicates doing this most effective way, and inform
user which textbox'es conflict with each other. ive done my job. some parts
are turkish (in alert message) but i think, it would be easly unserstood.
here are my codes. all advices (espessialy about performanse greatly
appreciated.)

<html>
<head>
<title></title>
<script type="text/javascript">
var caseSensitive = false;
function getAllTextInputs(beginningId)
{
aryResult = new Array();
var allTextInputElements = document.getElementsByTagName('input');
var count = 0;
for(var i = 0; i < allTextInputElements.length; i++)
{
var currentElement = allTextInputElements[i];
if(currentElement.type == "text" &&
currentElement.id.indexOf(beginningId) == 0)
{
aryResult[count++] = allTextInputElements[i];
}
}
return aryResult;
}

function getUniqueInputs(aryTextInputs)
{
var aryUniqueInputs = new Array();

for(var i = 0; i < aryTextInputs.length; i++)
{
var tempUniqueArray = null;
for(var j = i + 1; j < aryTextInputs.length; j++)
{
var xValue = caseSensitive ? aryTextInputs[i].value :
aryTextInputs[i].value.toLowerCase();
var yValue = caseSensitive ? aryTextInputs[j].value :
aryTextInputs[j].value.toLowerCase();
if(xValue == yValue)
{
if(tempUniqueArray == null)
{
tempUniqueArray = new Array();
tempUniqueArray.push(aryTextInputs[i]);
}
tempUniqueArray.push(aryTextInputs[j]);
aryTextInputs.splice(j, 1);
j--;
}
}
if(tempUniqueArray != null)
{
aryUniqueInputs.push(tempUniqueArray);
}
}
return aryUniqueInputs;
}

function getAlertMessage(aryUniqueInputs)
{
var alertMessage = 'Aşşağıdaki girişler aynı değeri içermekte :';
for(var i = 0; i < aryUniqueInputs.length; i++)
{
alertMessage += "\r\n";
for(var j = 0; j < aryUniqueInputs[i].length; j++)
{
alertMessage += aryUniqueInputs[i][j].friendlyName != null ?
aryUniqueInputs[i][j].friendlyName : aryUniqueInputs[i][j].id;
if(j != aryUniqueInputs[i].length -1)
alertMessage += ", ";
}
}
return alertMessage;
}

function validate_Form()
{
debugger;
var aryTextInputs = getAllTextInputs('kod');

var aryUniqueInputs = getUniqueInputs(aryTextInputs);

if(aryUniqueInputs.length != 0)
{
var alertMessage = getAlertMessage(aryUniqueInputs);
alert(alertMessage);
return false;
}
else
return true;
}
</script>
</head>
<body>
<form onsubmit="return validate_Form();">
Alan 1<input type="text" id="kod00" friendlyName="Alan 1"><br>
Alan 2<input type="text" id="kod01" friendlyName="Alan 2"><br>
Alan 3<input type="text" id="kod02" friendlyName="Alan 3"><br>
Alan 4<input type="text" id="kod03" friendlyName="Alan 4"><br>
Alan 5<input type="text" id="kod04" friendlyName="Alan 5"><br>
Alan 6<input type="text" id="kod05" friendlyName="Alan 6"><br>
Alan 7<input type="text" id="kod06" friendlyName="Alan 7"><br>
Alan 8<input type="text" id="kod07" friendlyName="Alan 8"><br>
<input type="submit">
</form>
</body>
</html>
Nov 17 '05 #3
<"The Crow" <q>> wrote:
i have a arraylist. say it contains integer values. i want to be able to
inform user, which indexes in the array contain same values. but there can
be N different values, and M different indexes having the same value.
actually i will do this with javascript array to inform user which input's
have same value. but i think its not a matter.


Given that you're not actually using C#, I *strongly* suggest you ask
on a JavaScript newsgroup instead. Answers here are less likely to be
from JS experts, and so could easily be wrong or less than optimal.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
the algorithm was more important for me. it doesnt matter c# or js.
Nov 17 '05 #5
<"The Crow" <q>> wrote:
the algorithm was more important for me. it doesnt matter c# or js.


But which algorithm you use may well depend on the platform - different
platforms have different functionality available.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
the functionality available will not different. come on, this is just adding
and removing values from/to array and maybe creating another array. it may
be a matter if you use C array but not for a modern language...
the reason why i posted this message to this group is, i think jscript
programmers does not code alghorithms most of their time.
Nov 17 '05 #7
<"The Crow" <q>> wrote:
the functionality available will not different. come on, this is just adding
and removing values from/to array and maybe creating another array. it may
be a matter if you use C array but not for a modern language...
the reason why i posted this message to this group is, i think jscript
programmers does not code alghorithms most of their time.


No, the functionality available may well be different. For instance,
JavaScript may or may not have a "Set" implementation (like Java does)
but .NET certainly doesn't - you generally use a Hashtable and either a
fixed value or use the key as the value.

Similarly, JS may or may not have the equivalent of .NET's
Array.IndexOf(Array, object, int).

In general terms, you could go through each element of the array (or
ArrayList - you seem to be using the two terms as if they're
interchangable), and either find the index of the first array element
*after* that source element which is equal to it, *or* you could build
up a Set/Hashtable of "elements seen so far".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
see my code above, can you see a hash table or "set" implementation.. dont
tell me doing so is more effective. because inserting to hashtable is costly
operration regarding to simply adding to array (btw i know array is not
arraylist.). and i wont do concurrent reads that will give me back lost cpu
cycles.. so hashtable is not efficient.
Nov 17 '05 #9
<"The Crow" <q>> wrote:
see my code above, can you see a hash table or "set" implementation.. dont
tell me doing so is more effective. because inserting to hashtable is costly
operration regarding to simply adding to array (btw i know array is not
arraylist.).
That entirely depends on how the array is implemented. Guess what? That
depends on the platform. You can't add to an array in .NET - they're a
fixed size. So, if you're actually after a JavaScript solution then
once again, I'll suggest you ask in the JS newsgroup.
and i wont do concurrent reads that will give me back lost cpu
cycles.. so hashtable is not efficient.


Have you benchmarked this to find out whether it's actually an issue?
Just how big is your array in real life anyway? It's not worth worrying
about efficiency if the simplest code works fast enough. (Not that I
believe your code is the simplest anyway, but anyway...)

Note that if you have a large array (which is the only time it would
actually matter), especially with a significant number of duplicates,
that would be more efficient - finding a match in a hashtable is faster
than having to loop through the array. Your solution is always O(n^2) -
a hashtable version would be O(n) I believe - according to MSDN, both
inserting into a hashtable and retrieving from it are O(1) operations.

If you're absolutely determined to use the O(n^2) solution, you could
at least call toLowerCase on aryTextInputs[i] *outside* the "j" loop -
you're currently lower-casing the same string every time. (In fact, you
might want to consider creating a separate array and lower-casing all
the values *once*, to avoid doing it within the loop at all.)
Anyway, it seems you've already decided that your solution is the best
one and you're unwilling to listen to my comments, so we might as well
call a halt here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10
lots of thanks for all your comments. and i dont think my solution is the
best. but even msdn says O(1) and my solution seems to be O(n^2), i think
inserting to hashtable is costly operation then mine because it will do a
lot of decisions and searching.. i agree rest of your comments.
Nov 17 '05 #11
<"The Crow" <q>> wrote:
lots of thanks for all your comments. and i dont think my solution is the
best. but even msdn says O(1) and my solution seems to be O(n^2), i think
inserting to hashtable is costly operation then mine because it will do a
lot of decisions and searching.. i agree rest of your comments.


Your solution may well be faster for small arrays, but will degrade
nastily when you have to search over larger ones. My solution will be
slower for small arrays, but won't degrade nearly as badly.

If the arrays are small though, do you really care about performance?
It's going to be blazingly quick whichever version you choose - just
how often are you expecting to do this, anyway?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
3
by: Brian | last post by:
Any reason why random.choice would return the first value in a list everytime?
3
by: uNConVeNtiOnAL | last post by:
Why doesn't this work - no errors, just no value when text is in textbox var fn=document.forms.elements.value; Thanks Tom
9
by: allenj | last post by:
DB2 UDB 7.2 WSE Fixpak 9 Linux Red Hat 7.3 I have some library code (written in Java, if that matters) that processes maintenance screens that are presented to the end-users as forms in a...
6
by: John Veldthuis | last post by:
I have an ArrayList set up with a set of class objects. The class has 3 strings in it and are as follows ProdID GSP Description Okay now problem with getting all these into the list and...
5
by: Mitchell S. Honnert | last post by:
Is there a way, given the full path of a folder on a network, that one can programatically tell if you have Read access to that folder? I have an application where the user is able to select a...
7
by: ravi | last post by:
Hi, I am a newbie to javascript. Iam working on a page that has two radio buttons and a tree. I do not have my tree nodes to be selectable. Iam working on Firefox. I have the firebug...
7
by: john_smith_1221 | last post by:
Hello, I need to use the rand() function to generate a random value, I already know how to do it with srand(time(NULL)) and its "randomness" is sufficient for me, the problem is my code requires to...
4
by: Octo Siburian | last post by:
Hii all, i have a problem to get every second row in a table with condition 1 column has a same value, example: i have 1 table is called 'HP' with column id,nik,hp,status_aktif id | nik |...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.