[问答题]

```Array.prototype.distinct = function() {
var ret = [];
for (var i = 0; i < this.length; i++)
{
for (var j = i+1; j < this.length;) {
if (this[i] === this[j]) {
ret.push(this.splice(j, 1)[0]);
} else {
j++;
}
}
}
return ret;
}
//for test

//本题的思路是，用每个元素与其他的元素比较，相同则把那个元素存入数组
Array.prototype.distinct = function(){
var ret = [];
for(var i=1; i<this.length;i++){
for(var j=i+1;j<this.length;j++){
if(this[j]==this[i]){
ret.push(this.splice(j,1)[0]);
//这步骤是最重要的this.splice(j,1)删除重复的元素，splice返回的是被删除的数组，加[0]就是
//这个被删除的元素，ret.push(这个元素)，把这个重复的元素加入到数组ret中
}
}
}
}
//test
document.write([1,2,3,1,2,2,2,3,5,6].distinct())

Array.prototype.distinct=function(){
var arr=[];
var obj={};
for(var i=0;i<this.length;i++){
if(obj[this[i]]==undefined)
obj[this[i]]=this[i];
else if(obj[this[i]])
arr.push(this[i]);
}
return arr;
}

```  Array.prototype.distinct = function (){
var ret = [];
for(var i=0;i<this.length;i++){
if(this.indexOf(this[i])!=i){
ret.push(this[i]);
}
}
return ret;
}
```

```Array.prototype.distinct = function() {
var res = [];
for (var i = 0; i < this.length;)
{
if(this.indexOf(this[i]) !== this.lastIndexOf(this[i])){
res.push(this[i]);
this.splice(i,1);
}
else{
i++;
}
}
return res;
}```

```Array.prototype.unique = function () {
var arr = this;
var ret = [];
var len = arr.length;
var tmp = {};
for (var i = 0; i < len; i++) {
if (!tmp[arr[i]]) {
tmp[arr[i]] = 1;
}else{
ret.push(arr[i]);
}
}
return ret;
}

Array.prototype.distinct=function(){
var noReArray=[];
var reItem=[];
var i=0;
while (this[i]!=undefined) {
for(var j=0;j<noReArray.length;j++){
if (this[i]==noReArray[j]) {
reItem.push(this[i]);
this.splice(i,1);
break;
}
}
if (j==noReArray.length) {
noReArray.push(this[i]);
i++;
}
}
return reItem;
}

var myArray=[1,1,1,12,12,12,'a','b','ba','ba','bc','"a"','e','1','2','2','"3"'];

console.log(myArray.distinct());
console.log(myArray);

Array.prototype.distinct=function(){
var reItem=[];
var i=0;
while(this[i]!=undefined){
if (this.indexOf(this[i])!=i) {
reItem.push(this[i]);
this.splice(i,1);
}
else{
i++;
}
}
return reItem;
}

var myArray=[1,1,1,12,12,12,'a','b','ba','ba','bc','"a"','e','1','2','2','"3"'];

console.log(myArray.distinct());
console.log(myArray);

```Array.prototype.distinct = function() {

var delArr = [],
i, j,
target, // 用于缓存比对基准值
hasPushed; // 标识：判断是否已被压入到返回的数组

// 仿照冒泡排序进行遍历
for (i = 0; i < this.length; i++) {
target = this[i]; // 缓存比对基准值
hasPushed = false; // 重置标识

for (j = i + 1; j < this.length; j++) {
if (this[j] === target) {

this.splice(j, 1);

// 最为关键的一步！上述操作导致数组长度减少1，因此需要在原基础上再执行一次循环
j--;

if (!hasPushed) {
delArr.push(target);
hasPushed = true;
}
}
}
}

return delArr;
};

var arr = [1, 1, 2, 3, 3, 4, 2, 3, 2, 1, 2, 5, 5, 3];
var newArr = arr.distinct();

console.log(arr); // [1, 2, 3, 4, 5]
// 如果上面没有 j--，那么输出 arr => [1, 2, 3, 4, 2, 5]

console.log(newArr); // [1, 2, 3, 5]```

```Array.prototype.unique=function(){
return [new Set(this)]
}```

Array.prototype.distinct = function(){
var newArr = [ ];
forEach(function(ele){
if(Array.indexOf(item) !== Array.lastIndexOf(item) && newArr.indexOf(item)){
newArr.push(item);
}
})
return newArr
}

Array.prototype.distinct = function() {
var li = [];
var same = [];
for (let i = 0; i < this.length; i++) {
if (!li.includes(this[i]))
li.push(this[i]);
else same.push(this[i]);
}
return same;
};
var numbers = [2, 3, 3, 2, 6, 9, 8, 7, 5, 2, 1, 0];
console.log(numbers.distinct());

Array.prototype.distinct = function() {
var ans = [];
for(var i = 0; i<this.length; i++){
for(var j = i+1; j<this.length-1;j++){
if(this[i] === this[j] ){
ans.push(this.splice(j,1)[0]);
j--
}
}
}
return this;
}

document.write([1,2,3,1,2,2,2,2,3,3,4,5,6].distinct())
//上面测试例结果为：1,2,3,4,5,6

Array.protoType.delete = function(arr){
return Array.from(new Set(arr))

console.log(arr.length)
Array.prototype.distinct = function() {
var arr2 = [];
var obj = {};
for(var i = 0; i < this.length; i++) {
var type=typeof(this[i])
if(type==='object'){
if(Array.isArray(this[i])){
var str=this[i].join('')
if(obj[str]==undefined){
obj[str]=1
}else{
obj[str]+=1
arr2.push(this[i])
}
}else{
var str=JSON.stringify(this[i])
if(obj[str]==undefined){
obj[str]=1
}else{
obj[str]+=1
arr2.push(this[i])
}
}
}else if(type==='function'){
var str=this[i].toString()
if(obj[str]==undefined){
obj[str]=1
}else{
obj[str]+=1
arr2.push(this[i])
}
}
else{
if(obj[this[i]] == undefined){
obj[this[i]] = 1;
}else{
obj[this[i]]+=1;
arr2.push(this[i])
}
}
}

return arr2;
}
console.log(arr.distinct());

Array.prototype.distance=function(){
var newArr=[];
for(var i=0;i<(arr.length-1);i++){
for(var j=(i+1);j<arr.length;j++){
if(arr[i]==arr[j]){
if(newArr){
var value=0;
for(var n=0;n<newArr.length;n++){
if(newArr[n]==arr[i]){
value=1;
}
}
if(value==0){
newArr.push(arr[i]);
}
}else{
newArr.push(arr[i]);
}
}
}
}
return newArr;
};

```来个有创意的：利用一次循环，对象属性唯一的原则实现
Array.prototype.distinct =function() {
var obj = {};
for( var i=0; i<= Math.ceil(this.length/2); i++ ){
if( this.indexOf(arry[i]) != arry.lastIndexOf(arry[i]) ){
obj[this[i]] = true;
}
}
return Object.keys(obj);
}

// Test
var arry = [1, 2, 'q', 1, 3, '1', 'q', 3, 1];
console.log(arry.distinct()); // ["1", "3", "q"]```

Array.prototype.distinct = function() {
for (var i = 0; i < this.length; i++) {
for (var j = 0; j < this.length; j++) {
if (this[i ] === this[j] && i != j)
this.splice(j,1);
}
}
return this;
}
var array = ['a','b','c','d','b','a', 0, 0, "0", null, null];
var b = {a: "ss"};
var c = {x: "tt"};
array.push(b);
array.push(b);
array.push(c);
array.push(c);
console.log(array.distinct());

```Array.prototype.delRepeat = function () {
var result = [];
var i,
len = this.length,
delarr=[];
for(i = 0;i<len;i++){
if(result.indexOf(this[i])===-1){
result.push(this[i]);
}else{
delarr.push(this[i]);
console.log('del '+i+':'+this[i]);
this.splice(i,1);
i--;
len--;
}
}
return delarr;
}```

Array.prototype.duplicate=function(){
var _this=this;
var result=[];  //返回值是一个包含被删除的重复条目的新数组。
for(var i=0;i<_this.length;i++){
for(var j=i+1;j<_this.length;j++){
if(_this[i]===_this[j]){
result.push(_this[j]);
_this.splice(j,1);//在原数组中删除该元素
}
}
}
return result;
}

var arr = [1,2,2,4,4,2,1,5];
console.log(arr);

[].constructor.prototype.distinct=function(){
var arr=[];
this.map(function(d){
(arr.indexOf(d)<0) && arr.push(d)
});
return arr;
};

var arr=[1,3,1,2,2,6,7,4,9,8,4]
arr=(arr.sort().join(',')+',').replace(/(.+,)\1+/g,'\$1').split(','),arr.pop();//一行代码搞定
return arr;

[].constructor.prototype.distinct=function(){ var str=(this.sort().join(',')+',').replace(/(.+,)\1+/g,'\$1'),str=str.substring(0,str.length-1),arr1=str.split(',');//一行代码搞定 return arr1; }

```Array.prototype.distinct = function() {
var tmpArray = [];
var tmpCount = 0;
for (var i = 0; i < this.length; i++) {
tmpCount = 0;
for (var j = i + 1; j < this.length; j++) {
if (this[i] === this[j]) {
tmpCount += 1;
}
}
if (!tmpCount) {
tmpArray.push(this[i]);
}
}
return tmpArray;
}
var testArray= [1,1,2,2,3,3,3,31,1,4,4,9];
console.log('before:',abcde);
console.log('after:',abcde.distinct());```

228条回答 29641浏览

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题