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: - // creates a list of all keywords and the removes any dublicates and printes the results
-
getKeywords : function() {
-
-
var keywords = articleHolder.getElementsByTagName('span');
-
var savedKeywords = [];
-
for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {
-
savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
-
}
-
-
if (allKeywords > 0) {
-
var para = document.createElement('p');
-
var strong = document.createElement('strong');
-
para.appendChild(strong).appendChild(document.createTextNode('Keywords: '));
-
-
savedKeywords.sort();
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
-
savedKeywords.splice(n,1);
-
-
}
-
else {
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
}
-
}
-
-
-
}
-
-
articleHolder.insertBefore(para, document.getElementById('copyright'));
-
}
Feb 19 '07
13 2002
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
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.
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.
- Array.prototype.hasAny= function(wot){
-
var L= this.length, cnt= 0;
-
for(var i= 0; i< L; i++){
-
if(A[i]=== wot) ++cnt;
-
}
-
return cnt;
-
}
-
Array.prototype.unique= function(){
-
var A= [];
-
var L= this.length;
-
for(var i= 0; i< L; i++){
-
var tem= this[i];
-
if(A.hasAny(tem)== 0) A.push(tem);
-
}
-
return A;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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
|
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)
|
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?
| |
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...
|
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...
|
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:
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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();...
| |
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...
| |