Pick the object that appears only once from the array

Source: Internet
Author: User

Go to a company for an interview ~

The problem with technical questions is to give an array of numbers that appear only once in the box.

Here is the method I gave, not the normal algorithm. There is also no consideration for the complexity of controls.

If you are Daniel, please write your algorithm in the reply. Welcome to Interactive

public static void Main (string[] args) {        int array[]={1,3,5,7,9,1,3,5,7};        int length=array.length;        StringBuilder sb= new StringBuilder ();        for (int i=0;i<length;i++) {            sb.append (array[i]);        }        for (int i=0;i<length;i++) {            int firstposition=sb.indexof (array[i]+ "");            int Endposition=sb.lastindexof (array[i]+ "");            if (firstposition==endposition)                System.out.println ("occurs once:" +array[i]);}    }


Bug raised for 1 floor

I make changes such as the following,


<span style= "Font-size:18px;color: #000000;" >public void Find () {        int array[]={11,13,55,77,99,21,13,55,77};        int length=array.length;        StringBuilder sb= new StringBuilder ();        for (int i=0;i<length;i++) {            sb.append (array[i]+ "s");        }        for (int i=0;i<length;i++) {            int firstposition=sb.indexof (array[i]+ "s");            int Endposition=sb.lastindexof (array[i]+ "s");            if (firstposition==endposition)                System.out.println ("occurs once:" +array[i]);            else                System.out.println ("appears multiple times:" +array[i]);}    } </span>


Pick the object that appears only once from the 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.