Files
hello-algo/ja/codes/javascript/chapter_searching/hashing_search.js
Yudong Jin d7b2277d2b Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
2026-03-30 07:30:15 +08:00

46 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: hashing_search.js
* Created Time: 2022-12-29
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
const { arrToLinkedList } = require('../modules/ListNode');
/* ハッシュ探索(配列) */
function hashingSearchArray(map, target) {
// ハッシュテーブルの key: 目標要素、value: インデックス
// ハッシュテーブルにこの key がなければ -1 を返す
return map.has(target) ? map.get(target) : -1;
}
/* ハッシュ探索(連結リスト) */
function hashingSearchLinkedList(map, target) {
// ハッシュテーブルの key: 目標ード値、value: ノードオブジェクト
// ハッシュテーブルにこの key がなければ null を返す
return map.has(target) ? map.get(target) : null;
}
/* Driver Code */
const target = 3;
/* ハッシュ探索(配列) */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// ハッシュテーブルを初期化
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: 要素、value: インデックス
}
const index = hashingSearchArray(map, target);
console.log('目標要素 3 のインデックス = ' + index);
/* ハッシュ探索(連結リスト) */
let head = arrToLinkedList(nums);
// ハッシュテーブルを初期化
const map1 = new Map();
while (head != null) {
map1.set(head.val, head); // key: ード値、value: ノード
head = head.next;
}
const node = hashingSearchLinkedList(map1, target);
console.log('目標ノード値 3 に対応するノードオブジェクトは', node);