[JAVA] I tried to solve AOJ's Binary Search

Hello This is skanehira from Global Sense Co., Ltd..

This time, I tried to solve AOJ's Binary Search. When I saw the title, I thought it would be difficult It was unexpectedly easy ...



In summary Output how many values in the 4th row are included in the 2nd row. It is the content.


To be honest, I don't need to enter the first and third lines ... I don't want to bother to skip it.

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        try (Scanner scan = new Scanner(System.in)) {

            //Store the search target in map
            Map<String, String> map = new HashMap<String, String>();
            for (String num : scan.nextLine().split(" ")) {
                map.put(num, "");


            //Count if there is a search target
            int count = 0;
            for (String num : scan.nextLine().split(" ")) {
                if (map.containsKey(num)) {




** How to solve **

for (int i : targetNums) {
    for (int j : nums) {
        if (i = j) count++;

Store the 2nd and 4th lines in an array I wondered if I could go by comparing while spinning around. It is inefficient to double the for statement I decided to use map.

The process should be faster if you use the map key as the search target and perform a key check! (Although not verified)

I wonder if there is another more efficient way ...

Recommended Posts

I tried to solve AOJ's Binary Search
I tried to solve AOJ's Small, Large, or Equal
[Java] I tried to implement Yahoo API product search
I tried to verify yum-cron
I tried to chew C # (indexer)
I tried something called recursive search
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried to solve the problem of Google Tech Dev Guide
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to summarize about JVM / garbage collection
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
Introduction to algorithms with java-Search (Full search, Binary search)
I tried to manage struts configuration with Coggle
Binary search binary search method
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
Method to search
I tried to develop a DUO3.0 study website.
I tried to chew C # (basic of encapsulation)
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried Spring.
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
[API] I tried using the zip code search API
[Ruby basics] I tried to learn modules (Chapter 1)
I tried tomcat
Binary search method
I tried to output multiplication table in Java
I tried youtubeDataApi.
I tried to link JavaFX and Spring Framework.
I tried refactoring ①
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java