HZFE / algorithms-solving

1 stars 0 forks source link

2022-08-23 #25

Open github-actions[bot] opened 2 years ago

gongpeione commented 2 years ago

217 Contains Duplicate

/*
 * @lc app=leetcode id=217 lang=typescript
 *
 * [217] Contains Duplicate
 */

// @lc code=start
function containsDuplicate(nums: number[]): boolean {
    const obj = {};

    for (let i = 0; i < nums.length; i++) {
        if (!obj[nums[i]]) {
            obj[nums[i]] = true;
            continue;
        } else {
            return true;
        }
    }

    return false;
};
// @lc code=end

Nickname: Geeku From vscode-hzfe-algorithms

gongpeione commented 2 years ago

242 Valid Anagram

/*
 * @lc app=leetcode id=242 lang=typescript
 *
 * [242] Valid Anagram
 */

// @lc code=start
function isAnagram(s: string, t: string): boolean {
    if (s.length !== t.length) return false;

    const so = {};
    const jo = {};

    for (let i = 0; i < s.length; i++) {
        so[s[i]] ? (so[s[i]] += 1) : (so[s[i]] = 1);
        jo[t[i]] ? (jo[t[i]] += 1) : (jo[t[i]] = 1);
    };

    for (const key in so) {
        if (so[key] !== jo[key]) return false;
    };

    return true;
};
// @lc code=end

Nickname: Geeku From vscode-hzfe-algorithms

Akiq2016 commented 2 years ago
// @lc code=start
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function (nums) {
  const map = new Map();
  let result = false;

  for (let i = 0; i < nums.length; i++) {
    if (map.get(nums[i])) {
      result = true;
      break;
    } else {
      map.set(nums[i], true);
    }
  }

  return result;
};
// @lc code=end