View Full Version : unknown arrays

May 11th, 2014, 03:53 AM
I have a homework assignment I am stuck on. It wants me to find unique unknown values from a file and output them. I don't understand how I'm supposed to find the unique values when i don't know what they are and put them into an array. Please, if there is anyone who understands this or can help me.

Philip M
May 11th, 2014, 11:37 AM
Unique values means those that occur only once in a list. In other words, the aim is to eliminate duplicates.

If you do not know the meaning of unique I would suggest that this assignment is far beyond your current abilities.

This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. However, I will make an exception as the code may be useful to others:-

<script type = "text/javascript">

var text = "zebra cat dog cat horse monkey cat dog";
var array = text.split(" ");
// var array = text.toLowerCase().split(" "); // to make case insensitive
alert (array)
alert (eliminateDuplicates(array));

function eliminateDuplicates(arr) {
var len=arr.length;
var out=[];
var obj={};

for (var i=0;i<len;i++) {obj[arr[i]]=0}
for (var i in obj) {out.push(i)}
out.toString(); // convert object to string
alert (out); // unsorted
out.sort(); // sort the array out if required
return out;


But I doubt if your teacher willl accept that this is your own work.

The code I have provided works in all browsers. If you only wish for support by modern browsers (IE9+) you could use a one-liner:-

alert (array.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]))