Find duplicate elements in an existing vector or ArrayList or array & to the existing vector or ArrayList or array

Source: Internet
Author: User

Directly on the code:
Public Static voidMain (string[] args) {List<Integer> list =NewVector<integer> (20); for(inti = 0; I < 10; i++) {List.add (i% 3); List.add (i% 6); List.add (i# t); List.add (i% 4); } System.out.println ("Original element:" +list); List<Integer> reList =NewVector<integer> (20); for(inti = 0; I < List.size ()-1; i++) { for(intj = i + 1; J < List.size (); J + +) { if(List.get (i) = =List.get (j)) {Relist.add (List.remove (i)); I--; Break; } } }//System.out.println ("Go to Weight after:" + list);//System.out.println ("repeating element:" + reList);//Output://primitive elements: Primitive elements: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 0, 2, 0, 3, 1, 3, 1, 4, 0, 0, 2, 5, 1, 1, 0, 0, 0, 2, 1, 1, 1, 3, 2, 2, 0, 0, 0, 3, 1, 1]//go back: [4, 5, 2, 0, 3, 1]//repeating elements: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 0, 2, 0, 3, 1, 3, 1, 0, 0, 2, 1, 1, 0, 0, 0, 2, 1, 1, 1, 3, 2] //de-weight Method 2Set<integer> Intset =NewHashset<integer>(list); System.out.println ("Set to go back" +intset);//Output://primitive elements: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 0, 2, 0, 3, 1, 3, 1, 4, 0, 0, 2, 5, 1, 1, 0, 0, 0, 2, 1, 1, 1, 3, 2, 2, 0, 0, 0, 3, 1, 1]//set de-weight after [0, 1, 2, 3, 4, 5]//ArrayList<integer> notrelist =NewVector<integer> (20); int[] Intarr =New int[]{1, 2, 4, 5, 6, 3, 4, 2, 3, 4, 34, 5, 23, 5, 2, 3, 4, 3, 3,99}; for(inti = 0; i < intarr.length; i++) {//if not calculated notrelist with I < intarr.length to I < intarr.length-1 if(!notrelist.contains (Intarr[i])) {Notrelist.add (intarr[i]); } for(intj = i + 1; J < Intarr.length; J + +) { if(Intarr[i] = =Intarr[j]) {Relist.add (intarr[i]); Break; }}} System.out.println ("Go to Weight after:" +notrelist); System.out.println ("Repeating element:" +reList);//Output://after weight: [1, 2, 4, 5, 6, 3, (+) , +]//repeating elements: [2, 4, 5, 3, 4, 2, 3, 4, 5, 3, 3]

// The list and array can also be used to turn each other: // List intlist=arrays.aslist (intarr); // integer[] intarr2=new integer[list.size ()]; // List.toarray (INTARR2);


}

Copyright, reproduced Please specify the source: http://www.cnblogs.com/langtianya/p/4676816.html

Find duplicate elements in an existing vector or ArrayList or array & to the existing vector or ArrayList or array

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.