Apply Java to remove duplicate numbers from a one-dimensional array without using any Java tool classes.

Source: Internet
Author: User

Yesterday, a friend suddenly asked me this question, for a moment I was speechless, just at the beginning is limited to no way to record the repeat position, as just contact with the Java language rookie, racked his brains to the most stupid way to achieve, but also hope the great God pointing out a better way.

public class Test01 {

public static void Main (string[] args) {
Test1 ();
}

/**
* Method Name: Test1
* Method Description: An array to remove duplicate numbers, not with the help of Java tools class, the method is more clumsy, but also ask the master pointing
* @param: Test01
* @return: void
* @Author: it Oriole
* @create Date:dec 9, 2014
*/
private static void Test1 () {
Int[] Arry1 = {1,2,1,3,1,23,14,12,321,2,5,5,1,32,2,13,12};

Statistics of repeated numbers
boolean[] repeat = new boolean[arry1.length];
for (int i = 0;i<arry1.length;i++) {
for (int j = 0;j<arry1.length;j++) {
if (I>j&&arry1[i]==arry1[j]) {//If the number is compared to each of his subsequent numbers if equal repeat[j] position is set to True
Repeat[j]=true;
}
}
}
Calculates the length of the second array
int lenth = 0;
for (int i = 0;i<repeat.length;i++) {
if (repeat[i]==false) lenth++;
}

int[] Arry2 = new int[lenth];//to define an array to receive non-repeating numbers
int temp = 0;//records the length of the second array
Transfer all the numbers to the new array
for (int i = 0;i<arry1.length;i++) {
if (Repeat[i]==false) {
Arry2[temp]=arry1[i];
temp++;
}
}
Print results Validation
for (int s:arry2) {
System.out.print (s+ ",");
}
SYSTEM.OUT.PRINTLN ("Total number of repetitions" + (repeat.length-lenth));

}
}

Apply Java to remove duplicate numbers from a one-dimensional array without using any Java tool classes.

Related Article

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.