473,804 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

duplicate data check from the array

30 New Member
hello,
I have the below code


Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.     var myArray =new Array("tulu","dudu","tulu","milu")
  3.     for(i=0;i<myArray.length;i++)
  4.         {
  5.             alert(myArray[i])
  6.  
  7.         }
  8. </script> 
In the above code the alert message prints "tulu" twice. But I need to avoid duplicacy. Can anybody will help me to fix it.

Thanks in advance
Mar 30 '08 #1
3 2468
hsriat
1,654 Recognized Expert Top Contributor
Try this...
Expand|Select|Wrap|Line Numbers
  1. function arrayUnique(array) {
  2.     var p, i, j;
  3.     for(i = array.length; i;){
  4.         for(p = --i; p > 0;){
  5.             if(array[i] === array[--p]){
  6.                 for(j = p; --p && array[i] === array[p];);
  7.                 i -= array.splice(p + 1, j - p).length;
  8.             }
  9.         }
  10.     }
  11.     return true;
  12. }
Harpreet
Mar 30 '08 #2
dasrasmikant
30 New Member
thank you very much hsriat. Its working.
could you explain me the above code..
Mar 30 '08 #3
hsriat
1,654 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. function arrayUnique(array) {
  2.     var p, i, j;
  3.  
  4.     /*for each ith index in array
  5.     from the last index to the 0th*/
  6.     for(i = array.length; i;){
  7.  
  8.         /*for each pth,
  9.         moving from the ith to the 0th*/
  10.         for(p = --i; p > 0;){
  11.  
  12.             /*if ith is same as pth*/
  13.             if(array[i] === array[--p]){
  14.  
  15.                 /*going down the index,
  16.                 until same value exists*/
  17.                 for(j = p; --p && array[i] === array[p];);
  18.  
  19.                 /*remove the matching index/ices 
  20.                 update the value of i according
  21.                 to the latest length of the array*/
  22.                 i -= array.splice(p + 1, j - p).length;
  23.             }
  24.         }
  25.     }
  26.     return true;
  27. }
Regards
Mar 30 '08 #4

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

Similar topics

2
5946
by: Eric Linders | last post by:
Hello, The code below is used to grab each row from a CSV file (except the first row) and insert it into a database, as long as it's not already in the DB. If I echo each row's data, it appears only once on the page. But, for some reason it inserts each row into the database 3 times. The code that echos the data, and the DB insertion code all appear within the same for loop.
3
3226
by: Giloosh | last post by:
Hello, i need some help if possible... i have a payments table with over 500 records i want to run a query that searches through the table spotting out any duplicate ID#'s and Dates. So basically it will run a search spotting out duplicate ID#'s. Than with in those ID#'s spot out duplicate dates. I could do this by eye and spend hours spotting out the duplicated dates, but it would be much easier and funner having a script do it for you.
3
13889
by: shine | last post by:
I am trying to add an integer array to a hashtable. While adding to hashtable I want to check the duplicates in array, the way I want to do is add the key(the integer stored in array) if a duplicate key is being added then the program should return the integer else it should return -1. Here is the piece of code I have written does the logic looks ok ? ------------------------------------------------------------------ class...
2
1487
by: Trond | last post by:
I have been trying to fig out how to build up an array with dates. When its generated i want to be able to remove duplicate dates. The dates is generated by FileInfo object that is scanning a folder for files. Here is some code for that. In this case i am only using a variable not an array. _info = new FileInfo(_fileName); base.Text = _info.LastWriteTime.ToShortDateString(); As i see it i must first generate the array with dates. Then...
9
5103
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test code below): (a)When adding BitArrays to the ArrayList and then looping through the ArrayList I seem to access only the latest added BitArray and I'm not exactly clear on best way to access each BItArray in the ArrayList (b)When I try to remove...
2
2867
by: Egbert Teeselink | last post by:
Hi there, I'm trying to process a form that I got from a JSP generated page. Basically, want to take out and process some of the data, and redirect the rest to a JSP page that will handle the rest. All works well, i traverse $_POST, writeout hidden input fields and autosubmit the form with a little javascript, a perfectly acceptable solution for our users. However, the data the JSP page sends contains one certain input with the same...
7
1862
by: ucfcpegirl06 | last post by:
Hello, I have a dilemma. I am trying to flag duplicate messages received off of a com port. I have a software tool that is supposed to detect dup messages and flag and write the text "DUP" on the GUI of the software tool to let the user know that a duplicate message was sent or received. Here is the code:
2
13478
by: Rich | last post by:
Hello, I am just checking if there is a property or technique for displaying or retrieving from a dataTable the top 1 row(s) for rows containing duplicate keys (IDs). I have to pull data from a sql server DB. The resultset contains 10 distinct rows but the keys are duplicate - like record 12345 is listed twice but distinct because some of the other data for each field is different. I only need to see one of the 2 rows for ID 12345...
2
2538
by: Harry Haller | last post by:
I want to duplicate the form data, edit it to remove some items (such as __EVENTTARGET, __EVENTVALIDATION, etc) and save it to a log. How can I make a duplicate (editable) copy of the Form Collection? The Form Collection is of type NameValueCollection is, underneath the covers, an object of type HttpValueCollection. The problem I have is that my copies have only one key item for a list (such as a radio button list) whereas the Form...
2
2586
by: raphael001 | last post by:
In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values already entered. Any help would be greatly appreciated. This is what i have so far except the code to check for duplicate values: Dim intarray() As Integer Dim intindex As Integer Dim intcount As Integer Dim intsearch As Integer Dim intlow As...
0
9591
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
10594
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
10343
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
10331
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
10087
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
7631
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
6861
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
5529
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...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.