/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var isNumberArray = require( '@stdlib/assert/is-number-array' );


// FUNCTIONS //

/**
* Comparator function to sort numeric values in ascending order.
*
* @private
* @param {number} a - first value
* @param {number} b - second value
* @returns {number} difference between `a` and `b`
*/
function ascending( a, b ) {
	return a - b;
}


// MAIN //

/**
* Returns the unique elements in an array.
*
* @private
* @param {Array} arr - input array
* @returns {Array} array of unique elements
*/
function unique( arr ) {
	var len;
	var out;
	var val;
	var i;
	var j;

	// Copy the array to avoid mutation:
	out = Array.prototype.slice.call( arr );
	len = out.length;

	// Sort array in ascending order:
	if ( isNumberArray( out ) ) {
		out.sort( ascending );
	} else {
		out.sort();
	}

	// Loop through the array, only incrementing a pointer when successive values are different. When a succeeding value is different, move the pointer and set the next value. In the trivial case where all array elements are unique, we incur a slight penalty in resetting the element value for each unique value. In other cases, we simply move a unique value to a new position in the array. The end result is a sorted array with unique values.
	for ( i = 1, j = 0; i < len; i++ ) {
		val = out[ i ];
		if ( out[ j ] !== val ) {
			j += 1;
			out[ j ] = val;
		}
	}
	// Truncate the array:
	out.length = j+1;
	return out;
}


// EXPORTS //

module.exports = unique;

@@ -633,12 +633,14 @@
 %0A// 
-MODU
+VARIAB
 LES 
@@ -651,67 +651,95 @@
 var 
-isN
+SORT_OPTS = %7B%0A%09'n
 um
-b
 er
-Array = require( '@stdlib/assert/is-
+ic': true // Use 
 num
-b
 er
--array' )
+ic collation such that %221%22 %3C %222%22 %3C %2210%22...%0A%7D
 ;%0A%0A%0A
@@ -789,24 +789,16 @@
 to sort 
-numeric 
 values i
@@ -839,22 +839,17 @@
 @param %7B
-number
+*
 %7D a - fi
@@ -868,22 +868,17 @@
 @param %7B
-number
+*
 %7D b - se
@@ -912,84 +912,211 @@
 er%7D 
-difference between %60a%60 and %60b%60%0A*/%0Afunction ascending( a, b ) %7B%0A%09return a - b
+negative number if %60a%60 comes before %60b, positive if %60a%60 comes after %60b%60, and %600%60 if they are equivalent%0A*/%0Afunction ascending( a, b ) %7B%0A%09return String( a ).localeCompare( String( b ), void 0, SORT_OPTS )
 ;%0A%7D%0A
@@ -1483,89 +1483,30 @@
 r:%0A%09
-if ( isNumberArray( out ) ) %7B%0A%09%09out.sort( ascending );%0A%09%7D else %7B%0A%09%09out.sort();%0A%09%7D
+out.sort( ascending );
 %0A%0A%09/

Switch to using localeCompare