Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use java to view numbers that appear only once

Shulou Source: shulou.com Published: 2022-06-01 01:00:41 10月02日 Update

This article "how to use java to view numbers that appear only once" is not quite understood by most people except programmers. Today, in order to make you better understand "how to use java to view numbers that appear only once", the editor summarizes the following content, which has certain reference value, detailed steps are clear, and details are handled properly. I hope you can gain something through this article. Now let's take a look at the details.

Given an integer array nums, there are exactly two elements that appear only once, and all other elements appear twice. Find out the two elements that appear only once.

Example:

Input: output:

Answer:

1public int [] singleNumber (int [] nums) {

2 int diff = 0

3 for (int num: nums) {

4 diff ^ = num

5}

6 diff & =-diff

7 int [] rets = {0,0}

8 for (int num: nums) {

9 if ((num & diff) = = 0) {

10 rets [0] ^ = num

11} else {

12 rets [1] ^ = num

13}

14}

15 return rets

16}

Parsing:

There are many solutions to this problem, only this one solution is listed here. If you want to know more, you can refer to the solutions of the previous two questions. The title says that only two numbers appear once, and the others appear twice. We put all the values into XOR once, and the result of XOR is that the two XOR values only appear once, because the result of both XOR values is 0. Because the two values are different, their XOR result must not be 0, that is to say, there must be a 1 on a bit where the result is converted into binary, so we can divide the array into two parts according to this 1. and the two that appear only once will definitely not be in the same group, according to this feature we can easily find these two values.

What can Java be used for? Java is mainly used in: 1. Web development; 2. Android development; 3. Client development; 4. Web page development; 5. Enterprise application development; 6. Java big data development; 7. Game development and so on.

Thank you for your reading. I hope you have a certain understanding of the key question of "how to use java to view numbers that appear only once". The specific usage still needs to be understood by hands-on experiments. Go and try it. If you want to read more articles about relevant knowledge points, welcome to follow the industry information channel!

Tags: Two development result number element content affirmation array more solution application proper same that is that is binary value enterprise only most. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Docker MariaDB OPPO Reno