Java array face Test (iv) __java

Source: Internet
Author: User

1 Delete a repeating element in an array
Ideas:
Most of the array problems can be solved by pointers and temporary variables.
Build a temp variable first
and build a temporary pointer n to the first element of the array
Assigns the first value of an array to the TEMP
See if the second value of the array is equal to Temp
If the equality continues
If you don't wait
Array pointer n++
arr[n]= This value
At the same time temp= this value

Public int[] Delete (int arr[]) {
    int len = arr.length-1;
    int n = 0;
    int temp = 0;
    for (int i = 1; i < Len; i++) {
        temp = arr[i];
        if (arr[i + 1] = = temp) {
            continue
        } else {
            n++;

            Arr[n] = arr[i + 1];
            temp = arr[i + 1];
        }}
    return arr;
}

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.