search
HomeJavaJavagetting StartedHow to find numbers that appear only once in an integer array

How to find numbers that appear only once in an integer array

You 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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.