JavaScript Union Find  No. 1 JS Solution Per LC 100% | 93%
\n\nUncommented Version:\n\nfunction UnionFind(length) {\n  const parent = new Array(length).fill(-1)\n  return {\n    find(node) {\n      if (parent[node] === 
![image](https://assets.leetcode.com/users/images/818e7f29-2214-432a-8ceb-daacd1f535ba_1631390676.9830525.png)\n\nUncommented Version:\n```\nfunction UnionFind(length) {\n  const parent = new Array(length).fill(-1)\n  return {\n    find(node) {\n      if (parent[node] === -1) return node\n      parent[node] = this.find(parent[node])\n      return parent[node]\n    },\n    union(node1, node2) {\n      const parent1 = this.find(node1)\n      const parent2 = this.find(node2)\n      if (parent1 !== parent2) parent[parent2] = parent1\n    },\n    getInfected(infection) {\n      const infectedGroup = this.find(infection)\n      let infected = 0\n      for (let i = 0; i < length; i++) {\n        if (this.find(i) === infectedGroup) infected++\n      }\n      return infected\n    }\n  }\n}\n\nconst getInfected = (graph, initial, ignore) => {\n  const uf = new UnionFind(graph.length)\n  for (let i = 1; i < initial.length; i++) {\n    uf.union(initial[i - 1], initial[i])\n  }\n  for (let node = 0; node < graph.length; node++) {\n    if (node === ignore) continue\n    for (let connection = node + 1; connection < graph.length; connection++) {\n      if (connection === ignore) continue\n      if (graph[node][connection]) uf.union(node, connection)\n    }\n  }\n  return uf.getInfected(initial[0])\n}\n\nconst minMalwareSpread = (graph, initial) => {\n  if (initial.length === 1) return initial[0]\n  initial.sort((x,y)=>x-y)\n  let numInfected = graph.length, nodeToRemove = initial[0]\n  for (let node of initial) {\n    const curInfected = getInfected(graph, initial.filter(check=>check!=node), node)\n    if (curInfected < numInfected) {\n      numInfected = curInfected\n      nodeToRemove = node\n    }\n  }\n  return nodeToRemove\n}\n```\n\n\n\nWith comments:\n```\n//Union Find is a Very useful DS that\'s fairly easy to learn to code.  This is a pretty basic implementation.\n//Can be made even faster with Union by Rank too.  A quick google search will return plenty of results and\n//explanations.  Basics are though, we can union all the nodes with connections, then run a find to see which\n//nodes belong to which groups.  Great for disconnected graphs.  We initialize an array that\'s the same length\n//as the # of nodes.  Array index = how we identify the node.  Value stored at that index indicates which group\n//the node belongs to.  If -1, it belongs to itself (it\'s in its own group).  If it\'s changed, it belongs in the same \n//group as the node at that index.  [-1,-1,-1,-1,2,-1] - All nodes belong in own groups except node 2 and 4. \n//The 2 at index four indicates node 4 and 2 are in same group. \n\nclass UnionFind {\n  constructor(n) {\n    this.parent = new Array(n).fill(-1);\n    this.len = n;\n  }\n  \n  find(node) {\n    if (this.parent[node] === -1) return node;\n    this.parent[node] = this.find(this.parent[node]);\n    return this.parent[node];\n  }\n  \n  union(node1, node2) {\n    const parent1 = this.find(node1), parent2 = this.find(node2);\n    if (parent1 !== parent2) this.parent[parent2] = parent1;\n  }\n  \n  //This is simply a helper function specific to this problem.  We can pass it an infected node,\n  //find will then tell us what group that node is in.  We then just check what group the rest of \n  //the nodes are in.  If they\'re in the same group, increment a counter and return.\n  getInfected(infection) {\n    const infectedGroup = this.find(infection);\n    let infected = 0;\n    for (let i = 0; i < this.len; i++) {\n      if (this.find(i) === infectedGroup) infected++;\n    }\n    return infected;\n  }\n}\n\nconst minMalwareSpread = function(graph, initial) {\n    if (initial.length === 1) return initial[0];\n\t\n\t//Sort gets by a \'trick\' edge case in the LC tests where initial [3,0].\n\t//Both infections have the same results, but they expect the 0 returned.\n    initial.sort((x, y) => x - y);\n\t\n    let minInfected = graph.length, toRemove = initial[0];\n\t\n\t//For each infection, we run our helper function to see how many nodes would ultimately\n\t//end up infected if the node were to be removed from the group.  We\'re looking for the lowest\n\t//total infected to see which node to remove.\n    for (let infection of initial) {\n        const numInfected = getInfected(graph, initial.filter(val => val !== infection), infection);\n        if (numInfected < minInfected) {\n            minInfected = numInfected;\n            toRemove = infection;\n        }\n    }\n    return toRemove;\n};\n\nfunction getInfected(graph, initial, ignore) {\n\n\t//UF is very simple to use.  Initialize, union everything with connections, \n\t//then use find to see which group individual nodes belong to.\n\t\n    const unionFind = new UnionFind(graph.length);\n\t\n\t//Union all infected.  This lets us identify all infected nodes more easily.\n    for (let infected = 1; infected < initial.length; infected++) {\n        unionFind.union(initial[infected], initial[infected - 1]);\n    }\n\t\n\t//Union all other nodes minus the one to be removed (ignore in this case).\n    for (let node = 0; node < graph.length; node++) {\n        if (node === ignore) continue;\n        for (let connection = node + 1; connection < graph.length; connection++) {\n            if (connection == ignore) continue;\n            graph[node][connection] && unionFind.union(node, connection);\n        }\n    }\n\t\n\t//Return our helper function on the union find class which just loops through\n\t//all nodes and gets a count of all that belong to the same group as the infection.\n    return unionFind.getInfected(initial[0]);\n}\n```
['Union Find', 'JavaScript']