d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 25 Fg For A Little Help > Javascript Arrays
Add Reply New Topic New Poll
Member
Posts: 277
Joined: Sep 27 2013
Gold: 0.00
Dec 15 2014 09:01pm
Here is a segment of code I have

for (index = 0; index < fullZipArray.length; index++) {
if (zipArray[index] == null) {
zipArray.push(fullZipArray[index];
}
for (index2 = 0; index2 < zipArray.length; index2++) {
if (zipArray[index2] != fullZipArray[index]) {
zipArray.push(fullZipArray[index];
}
}
}

I have two Arrays, zipArray is empty, fullZipArray has 1000 zip codes. I need to move one of each of the zip codes into the zipArray, so I need to remove the duplicates. the code above does not work at all and I have no clue how else to write it.

This post was edited by CondescendenceX on Dec 15 2014 09:06pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Dec 15 2014 09:24pm
There is a function for an Array in JavaScript called filter. You define a callback function to use for your filtering. We can use that to filter based on duplicates.

Code
var fullZipArray = ["11111", "22222", "33333", "44444", "44444", "55555"];
var zipArray = fullZipArray.filter(
function(element, index)
{
//indexOf will return the first index of element. If this element is a duplicate,
//it's index will not match the first encountered element
return fullZipArray.indexOf(element) == index;
}
);


This post was edited by Minkomonster on Dec 15 2014 09:24pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Dec 16 2014 12:41am
If you can't use java functions, you could also just go through the zipArray.

Pseudo code:
Code

for i = 0 to n - 1
char temp = array[i];
for j = i+1 to n
if zipArray[j] == temp
zipArray[1] = duplicate


then just go through the zipArray and any duplicates will no longer be a zip code and you can filter them out

It's O(n^2) but it should work

This post was edited by lopelurag on Dec 16 2014 12:45am
Member
Posts: 82
Joined: Nov 11 2014
Gold: 200.00
Dec 16 2014 02:04pm
You can also use a Hashtable or sort then remove duplicates for faster algorithms; I think it'd be O(1) and O(nlogn) if I remember correctly.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Dec 16 2014 10:12pm
Found this thread, https://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array

I liked this reply the most.

Quote
Uniq reduce while keeping existing order

Code
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];

var uniq = names.reduce(function(a,b){
    if (a.indexOf(b) < 0 ) a.push(b);
    return a;
  },[]);

console.log(uniq, names) // [ 'Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Carl' ]

// one liner
return names.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]);


Faster uniq with sorting

Code
var uniq = names.slice() // slice makes copy of array before sorting it
  .sort(function(a,b){
    return a - b;
  })
  .reduce(function(a,b){
    if (a.slice(-1)[0] !== b) a.push(b); // slice(-1)[0] means last item in array without removing it (like .pop())
    return a;
  },[]); // this empty array becomes the starting value for a

// one liner
return names.slice().sort(function(a,b){return a - b}).reduce(function(a,b){if (a.slice(-1)[0] !== b) a.push(b);return a;},[]);


Not familiar with JavaScript much but I'm surprised there was no mention of Set, though I'm unsure if it works the same as it does in other languages

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll