Array element de-weight

Source: Internet
Author: User

Problem Description: Implement an algorithm that returns an array that removes duplicate elements from the original array and preserves the order of the elements.

For example: input: 2,1,2,3,-9,5,-9,10,

Output: 2,1,3,-9,5,10,

The Java code is as follows:

1 ImportJava.util.*;2  classChuchong {3    Public Static int[] RemoveDuplicates (int[] Array) {4Map<integer, boolean> map =NewHashmap<integer, boolean> ();//used to store the value of an array element that is not duplicated, where key is an element value, value is a flag bit, true indicates existence, and false means that there is no5list<integer> values =NewArraylist<integer> ();//store non-repeating element values6 7      for(intValue:array) {8       if(Map.containskey (value))//determines whether each array value is in the collection map, and if so, skips the value or adds it to the collection map9         Continue;Ten  OneMap.put (Value,true);  A Values.add (value); -     } -  the     int[] result =New int[Values.size ()]; -      for(inti = 0; I < values.size (); i++) {//copies the elements in values into a fixed-length array -Result[i] =Values.get (i); -     } +     returnResult//returns a reference to the array being evaluated -   } + } A   at  Public classMain { -      Public Static voidMain (string[] args) { -         int[] a={2,1,2,3,-9,5,-9,10}; -System.out.print ("Original array is:"); -          for(inti=0;i<a.length;i++) -System.out.print (a[i]+ ","); in System.out.println (); -System.out.print ("The array after removing duplicate values is:"); to        int[] b=Chuchong.removeduplicates (a); +         for(inti=0;i<b.length;i++) -System.out.print (b[i]+ ","); the     } *  $}
View Code

The output is:

The original array is: 2,1,2,3,-9,5,-9,10,
The array after removing duplicate values is: 2,1,3,-9,5,10,

Array element de-weight

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.