473,406 Members | 2,707 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,406 software developers and data experts.

duplicate data check from the array

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 2448
hsriat
1,654 Expert 1GB
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
thank you very much hsriat. Its working.
could you explain me the above code..
Mar 30 '08 #3
hsriat
1,654 Expert 1GB
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
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...
3
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...
3
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...
2
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...
9
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...
2
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...
7
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...
2
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...
2
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...

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.