This question has already been answered:
- Returning unique values from array 2 responses
Is it possible to do without the str variable declared in the outer loop and directly compare arr[i] with result[i] ?
I have only one consideration about this, which would store the beginning of the outer loop in the variable str , in case of expansion or making changes to the function. Because, otherwise, after a full pass of the outer loop, the beginning will not be saved anywhere. But maybe I am I wrong? Let arr be an array of strings.
Write the function unique(arr) , which returns an array containing only unique arr elements. For example, something would be output an array of strings: krishna, hare, 8- ().
function unique(arr) { var result = []; nextInput: for (var i = 0; i < arr.length; i++) { var str = arr[i]; // для каждого элемента for (var j = 0; j < result.length; j++) { // ищем, был ли он уже? if (result[j] == str) continue nextInput; // если да, то следующий } result.push(str); } return result; } var strings = ["кришна", "кришна", "харе", "харе", "харе", "харе", "кришна", "кришна", "8-()" ]; console.log(unique(strings)); // кришна, харе, 8-()