473,811 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1961
>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 getAllTextInput s(beginningId)
{
aryResult = new Array();
var allTextInputEle ments = document.getEle mentsByTagName( 'input');
var count = 0;
for(var i = 0; i < allTextInputEle ments.length; i++)
{
var currentElement = allTextInputEle ments[i];
if(currentEleme nt.type == "text" &&
currentElement. id.indexOf(begi nningId) == 0)
{
aryResult[count++] = allTextInputEle ments[i];
}
}
return aryResult;
}

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

for(var i = 0; i < aryTextInputs.l ength; i++)
{
var tempUniqueArray = null;
for(var j = i + 1; j < aryTextInputs.l ength; j++)
{
var xValue = caseSensitive ? aryTextInputs[i].value :
aryTextInputs[i].value.toLowerC ase();
var yValue = caseSensitive ? aryTextInputs[j].value :
aryTextInputs[j].value.toLowerC ase();
if(xValue == yValue)
{
if(tempUniqueAr ray == null)
{
tempUniqueArray = new Array();
tempUniqueArray .push(aryTextIn puts[i]);
}
tempUniqueArray .push(aryTextIn puts[j]);
aryTextInputs.s plice(j, 1);
j--;
}
}
if(tempUniqueAr ray != null)
{
aryUniqueInputs .push(tempUniqu eArray);
}
}
return aryUniqueInputs ;
}

function getAlertMessage (aryUniqueInput s)
{
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 = getAllTextInput s('kod');

var aryUniqueInputs = getUniqueInputs (aryTextInputs) ;

if(aryUniqueInp uts.length != 0)
{
var alertMessage = getAlertMessage (aryUniqueInput s);
alert(alertMess age);
return false;
}
else
return true;
}
</script>
</head>
<body>
<form onsubmit="retur n validate_Form() ;">
Alan 1<input type="text" id="kod00" friendlyName="A lan 1"><br>
Alan 2<input type="text" id="kod01" friendlyName="A lan 2"><br>
Alan 3<input type="text" id="kod02" friendlyName="A lan 3"><br>
Alan 4<input type="text" id="kod03" friendlyName="A lan 4"><br>
Alan 5<input type="text" id="kod04" friendlyName="A lan 5"><br>
Alan 6<input type="text" id="kod05" friendlyName="A lan 6"><br>
Alan 7<input type="text" id="kod06" friendlyName="A lan 7"><br>
Alan 8<input type="text" id="kod07" friendlyName="A lan 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.co m>
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.co m>
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(A rray, 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.co m>
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.co m>
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

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

Similar topics

6
4301
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 while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and other with blanks. Thanks a lot.
3
1917
by: Brian | last post by:
Any reason why random.choice would return the first value in a list everytime?
3
2312
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
4201
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 browser. Because the code is generic, working against any table, I am dynamically generating UPDATE statements that update
6
4078
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 displaying them correctly and manipulating things except for one thing.
5
13307
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 number of search folders using the standard dialog control. There shouldn't be an issue with the search folder being on a local drive or a network drive. But if you don't have Read access to the folder, there's trouble. The user can see the...
7
1591
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 debugger attached. Actually this is about a problem iam trying to debug. And found what the problem is and iam looking for a hack/ workaround to fix the bug.
7
6223
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 have that in a struct constructor, which means that rand() will be called upon object instantiation, my code creates objects dynamically all at once, and that results in the SAME VALUE for that class member for each object, that obviously is...
4
2135
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 | hp | status_aktif 1 | 2010010269 | 08123456789 | 1 2 | 2010010269 | 08567891011 | 1
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10652
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10408
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7673
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5561
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.