How to find numbers that appear only once in an integer array
Sep 07, 2020 pm 05:50 PMYou can use the hashMap method to achieve this. The steps are as follows:
(Video tutorial recommendation: java course)
1 , The key in the HashMap stores the number of the array array, and the value stores the number of values ????in the array;
2. Traverse the HashMap, find the key whose Value value is equal to 1, and store it in the new array temp ;
3. Assign the values ??in the array temp to num1, num2;
The code is as follows:
import java.util.Map; import java.util.HashMap; import java.util.Set; public class Solution { public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) { Map<Integer,Integer> map=new HashMap(); for(int i=0;i<array.length;i++){ if(map.containsKey(array[i])){ int len=map.get(array[i]); map.put(array[i],len+1); }else{ map.put(array[i],1); } } int[] temp=new int[2]; int index=0; Set<Map.Entry<Integer, Integer>> sm=map.entrySet(); for (Map.Entry<Integer, Integer> entry : sm) { int t1=entry.getKey(); int t2=entry.getValue(); if(t2==1){ temp[index++] = t1; } } num1[0]=temp[0]; num2[0]=temp[1]; } }
For more tutorials, please visit java Getting Started Tutorial column.
The above is the detailed content of How to find numbers that appear only once in an integer array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

On May 7, our mobile phone manufacturer officially announced that our company’s GTNeo6 launch conference is scheduled for May 9. GTNoe6 is positioned as a "performance storm", aiming to stir up the mid-range machine situation. In addition, this conference will also be the first AI digital human conference in the mobile phone industry. At that time, Realme Vice President, Global Marketing President, and China President Xu Qi will appear at the press conference in the form of a digital human. Digital man Xu Qi According to the official introduction, Realme GTNoe6, codenamed "Hurricane", is faster and stronger. It will challenge the strongest third-generation Snapdragon 8s flagship and the strongest product in its class. Recently, the Realme GTNeo6 was found to be directly on the e-commerce platform. Some core configurations were exposed, showing that the machine is not only equipped with a Snapdragon 8s processor, but also supports 120W flash charging.

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

Three common algorithms for exchanging array key values ??in PHP have their own advantages and disadvantages: array_flip(): simple and efficient, but the values ??must be unique and cannot handle multi-dimensional arrays. Manual traversal: can handle multi-dimensional arrays and control exceptions, but the code is longer and less efficient. ksort()+array_keys(): can handle any type of array and control the sort order, but it is less efficient. Practical cases show that array_flip() is the most efficient, but when dealing with multi-dimensional arrays, manual traversal is more appropriate.

Comparison of the algorithm time complexity of arrays and linked lists: accessing arrays O(1), linked lists O(n); inserting arrays O(1), linked lists O(1)/O(n); deleting arrays O(1), linked lists O(n) (n); Search array O(n), linked list O(n).

The top four global virtual currency trading platforms in 2025 are: Binance: a leader in the industry, providing diversified trading options and innovative products. OKX: A huge user base, providing comprehensive cryptocurrency services. Gate.io: User-friendly, offering a wide range of cryptocurrency options. Bitget: Focus on derivatives trading and provides high leverage futures contracts.

In C++, an array is a fixed-size data structure whose size needs to be specified at creation time, whereas a vector is a dynamic-sized data structure whose size can be changed at runtime. Arrays use the [] operator to access and modify elements, while vectors use the push_back() method to add elements and the [] operator to access elements. Arrays need to use delete[] to release memory, while vectors use erase() to delete elements.
