Javascript practice-Remove repeated elements from Arrays

Source: Internet
Author: User

In JS, how does one remove repeated elements in a specified array?

This is a question developed by the Web Front-End of xx.com. Levin wanted to leave directly when he knew that he was about to do the question. Later, he thought about it. Since it was introduced by a friend, he could not play a big game. Now, by the way, I would like to share my views on the interview and reserve the right to speak in the comments. Haha.

In fact, I hate to do questions and hate exam-oriented education. This kind of stuff can, to some extent, test how a person is doing, but it is too formal. Let's take a look at a person's work, or let him do a few computer questions. At that time, with such a mentality, the questions were naturally done again... Now, it is very easy to think about those questions, that is, they are not used in actual work .. Even if you want to use it on a temporary basis, if you don't have a clue after thinking about it, there will also be Google's great gods. Of course, you can leave Google alone to answer questions. This is a way to understand and solve the problem, which varies from person to person. The goal is the same: understand the problem, solve the problem, and master the solution to the problem. I personally prefer to stand on the shoulders of my predecessors to see problems.

 

Okay. Let's talk about this JS question. I opened the firebug console and spent 30 seconds thinking about it and 30 seconds writing it. The result is as follows: (x is the target array, and Y is the result of removing repeated elements)

 

Code

  VaR  X  = [  1  ,  1  ,  3  ,  4  ,  5  ,  3  ];
VaR Y = [];
VaR Tarray = Function (I, arr ){
VaR Yap = False ;
For ( VaR J = 0 ; J < Arr. length; j ++ ){
If (ARR [J] = I) {yap = True ; Break ;};
}
If ( ! Yap) Arr. Push (I );
};
For ( VaR T = 0 ; T < X. length; t ++ ){
Tarray (X [T], y );
}
Alert (Y. Length );
Alert (Y. tostring ());

 

The answer is not unique and there is no standard answer.CodeYes. What if the array is a complex object array? What if the array contains multiple null objects? You need to know that JS contains many special and even bug phenomena. Alert ({}={}) to see what is going on...

Note: if anyone has a standard answer, please post it in the comments for everyone to learn.

The problem will not be taken into account by the author.

Do not study and do questions. The ability to efficiently solve problems in actual work is truly valuable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.