check array for duplicate values JS

hey,
I am loading from xml a NAME of an object and corresponding X,Y,Z values for the object.
These are placed in separate arrays for me to better control the data.
Some of these names are the same, but I am only interested in the last set where the name+x,y,z occur.

What I struggle with is: I have 4(I also have 3 for rota. but lets not over complicate it) arrays where the are some of the names being duplicates.In the code below the NameArray is ‘t’.

I wish to make an array where all the latest 't’s are only represented. And their corresponding values as well

e.g.
UNIQUE NAME|X|Y|Z|
UNIQUE NAME|X|Y|Z|
UNIQUE NAME|X|Y|Z|
UNIQUE NAME|X|Y|Z|
etc.

Code below shows where I gave up.

var CheckEntryArray : Array = new Array();
	//Instantiate all objects in XML with corresponding positions and rotations with final placements of objects
	for(var i : int=0; i < t.length; i++)
	{
		Instantiate(spawn, Vector3(xt[i], yt[i], zt[i]), Quaternion.Euler(xr[i],yr[i],zr[i]));
		spawn.name = t[i];
		
		CheckEntryArray.Push((t[i]),(xt[i]),(yt[i]),(zt[i]));
		
		//print(CheckEntryArray);
		
	}

You should never use the JS Array class; use a generic List instead. You can look in the docs for List for functions that will be useful, such as Contains.

–Eric

thanks for the tip.
I am now using Lists.
The case is that I have a list with name and the coordinates in 3 float lists.
I only want latest entry of the same NAME so I reverse my list and check for duplicates…like below

var Name :List.<String> = new List.<String>();
function Start(){
Name = GameObject.Find("_ScriptHolder").GetComponent(xml).nameList;

}
var updatedNameList : List.<String> = new List.<String>();
function CheckName()
{
		Name.Reverse();//Reversing order of strings('same' as looping backwards)
		
		for(var nameCheck:String in Name)
		{
			
			
			if(updatedNameList.Contains(nameCheck))
			{
				
				
				
			}
			else
			{
				updatedNameList.Add(nameCheck);
				//updatedXList.Add(xposCheck);
				print(nameCheck);
				
			}
		}
		updatedNameList.Reverse();
		print(updatedNameList.Count);
		
}

Now what I cant seem to figure out is:
to only add values from the other lists at the corresponding index. to new lists of floats.

I tried looping two lists in same for() ← unsuccessful
and nested would count wrongly

any ideas? :slight_smile:

best,

Use a Dictionary or a HashSet. Also for the sake of speed - don’t reverse a list and then enumerate, just use a for loop with an index that counts down.

Probably a Dictionary would be best, but if you need a List, I’d suggest using a class which contains the name and the coordinates, instead of separate parallel lists. You can use Linq to check the name property. For example, in a List of Vector2s, this gets all the entries where the x property is .5:

import System.Linq;

function Start () {
	var myList = new List.<Vector2>([Vector2(.5, 1.0), Vector2(.7, 1.1), Vector2(.5, 1.1)]);
	var matches = myList.Where (function(item) item.x == .5);
	for (match in matches) {
		Debug.Log (match);
	}
}

–Eric