473,810 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with an Array

6 New Member
I'm having trouble with an array of data created using the 'getElementsByT agName' call. I'm trying to remove duplicates from the Array. code follows:


Expand|Select|Wrap|Line Numbers
  1. // creates a list of all keywords and the removes any dublicates and printes the results
  2.     getKeywords : function() {
  3.  
  4.         var keywords = articleHolder.getElementsByTagName('span');
  5.         var savedKeywords = [];
  6.         for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {
  7.             savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
  8.         }
  9.  
  10.         if (allKeywords > 0) {
  11.             var para = document.createElement('p');
  12.             var strong = document.createElement('strong');
  13.             para.appendChild(strong).appendChild(document.createTextNode('Keywords: '));
  14.  
  15.             savedKeywords.sort();
  16.             for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
  17.                 alert(savedKeywords[n]);
  18.                 if (savedKeywords[n] == savedKeywords[n + 1]) {
  19.  
  20.                     savedKeywords.splice(n,1);
  21.  
  22.                 }
  23.                 else {
  24.                     para.appendChild(document.createTextNode(savedKeywords[n]));
  25.                     para.appendChild(document.createTextNode(', '));
  26.                 }
  27.             }
  28.  
  29.  
  30.         }
  31.  
  32.             articleHolder.insertBefore(para, document.getElementById('copyright'));
  33.     }
Feb 19 '07
13 2002
Saint48198
6 New Member
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
Feb 19 '07 #11
dorinbogdan
839 Recognized Expert Contributor
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
I'm sure that exist methods without sort, but they would be more complex.
If you really need, I will research for that deeper.
Feb 20 '07 #12
acoder
16,027 Recognized Expert Moderator MVP
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
Well, it should be possible, but it won't be as efficient. You can have two arrays. One contains the words, the second one is empty. Then as you loop through the first array, you call a function which checks in the second array (has the word already been added). If the word is already present it is ignored, otherwise it is added. It should be quite simple to implement.
Feb 20 '07 #13
mrhoo
428 Contributor
Expand|Select|Wrap|Line Numbers
  1.     Array.prototype.hasAny= function(wot){        
  2.         var L= this.length, cnt= 0;
  3.         for(var i= 0; i< L; i++){                
  4.             if(A[i]=== wot) ++cnt;        
  5.         }
  6.         return cnt;
  7.     }
  8.     Array.prototype.unique= function(){
  9.         var A= [];
  10.         var L= this.length;
  11.         for(var i= 0; i< L; i++){
  12.             var tem= this[i];
  13.             if(A.hasAny(tem)== 0) A.push(tem);
  14.         }
  15.         return A;
  16.     }
Feb 21 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1842
by: Alex Hopson | last post by:
Hi, I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am having trouble setting up some arrays for it. The original code, below, stores the cart items in a session variable array ($HTTP_SESSION_VARS = array();) basically this stores an associative array with the id number of the product and the qty (ie cart = qty of that product). This works fine, but I need to be able to have sub options AND colours for
1
13302
by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through the application's front end the file appears to be corrupt. The front-end application has a way of inserting files to the column, however when I analyze this in SQL Profiler the contents of the Byte array appear to be quite different than the one...
4
4033
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give it a try here... Okay, here's the deal: I am trying to read from unmanaged memory with a class type struct, this
2
1454
by: 4Ankit | last post by:
hey guys i am having trouble changing my array code to include a 'for/ in' structure the code i am trying to change is below: <script type="text/javascript"> var contents = new Array(3)
6
1448
by: _Skare_Krow_ | last post by:
I have a database with three columns. One is an atomic number, one is a pic url, and the other is a description. What I'm having trouble doing is putting the url in one table row and the description in another table row right below it like this... <tr<tdsome pic url</td<tdother pic url</td></tr> <tr><tdsome descripts </td<tdother descript </td></tr> whilest limiting the rows to about 10 cells each,,.... any ideas?
9
5620
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return String.Format("'{0}'", value) End Function Basically, I just want to surround each value with single quotes. However, I am having trouble getting this to work using the System.Array.ForEach method. This may be partially because I am not very...
0
920
by: bmerlover | last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender, System::EventArgs * e) function is called, it is suppose to make the Play button dissapear and the Pause Button Appear as soon as the Play button is clicked. I don't know why it calls the keystrokes function then goes through the while loop...
0
1161
by: jthep | last post by:
Hi, I'm trying to get user input for a record book but I'm having trouble as I think I'm not making the getline function read the buffer correctly. I have the following variables declared in main: char name, address, telno, input; int yearofbirth, choice, records; yearofbirth = 0; choice = -1; Then here's part of the code where it goes wrong:
3
1926
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes instead. The following is the java script that i am using and i am having trouble in using a class instead of a style tag because simply replacing the style=\"position:absolute;\" tag with class=\"posiAbs\" where in the external css ".posiAbs...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
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...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
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
10124
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...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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
6882
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();...
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.